Thread: prime numbering

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    28

    prime numbering

    i am trying to make a program that checks if the number is a prime number:
    Code:
    #include <stdio.h>
    
    int prime( int a )
    
    int main(){
    	int b;
    	printf("type in the  number:")
    	scanf("%d",&b);
    	if (prime( b )==b){printf("of course");}
    	else {printf("nope=no hope");}
    	getchar();
    	return 0;}
    
    int prime( int a ){
    	int x;
    	for(x=2;x<a;x++){
    		if(!(a%x==0)){return a;}}}
    but when i run it after typing in the number it quits
    pleasse help!!!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what your prime function should return if the number is prime and if it is not?

    And where is the second return?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    sorry but it really hard for me to read and consequently understand your code

    please see this.
    C Coding Standards

    Code:
    int testprime(int no)
    {
        int i;
        for(i=2;i<no;i++)
        {
            if((no % i) == 0)
            {
                return 0;            
            }
        }
        return 1;
    }
    so if 1 is returned then number is prime.
    Last edited by creeping death; 03-26-2009 at 11:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:06 PM
  2. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 04:32 PM
  4. Prime Wonder
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-07-2002, 11:49 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM

Tags for this Thread