Thread: problem with my prime number program

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    problem with my prime number program

    well this program was made so it could tell u the if the number entered is a prime number or not...but i aint workin'

    Code:
    #include <stdio.h>
    
    int prime_number ( int );
    
    int main()
    {
    
     int num1, sum;
      printf ("Enter a number:");
      scanf ("%d", &num1 );
    
             sum = prime_number ( num1 );
    
        if ( sum == 1){
        printf ("%d is not a prime number", num1 );
        else 
            if ( sum == 0 )
               printf ("%d is a prime number", num1)
    
    return 0;
    }
    
    int prime_number ( int a)
    {
    
       int num2, tot;
       num2 = a/2;
       tot = a/num2;
       
         if ( tot == 2 )
            return 1;
         else 
            return 0;
    }

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Code:
    if ( sum == 1){
    ....
    printf ("%d is a prime number", num1)
    Well for a start you don't close of the bracket at the end of your if statement and you don't have a semi colon to close off the statement on your printf.

    If it is just not compiling this should fix it. Otherwise let us know.

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Your logic isn't quite correct either as 2 and 19 are prime numbers, but the program will say they aren't.


    Definition: An integer p is called a prime number if the only positive integers that divide p are 1 and p itself.

    This function should do it, please note that you will have to include the math.h library for the functions floor and sqrt.
    Code:
    int prime_number(int n)
    {
      int i;
      for (i = 2 ; i <= floor(sqrt(n)) ; i++) {
        if (n % i == 0) {
          return(0);
        } 
      } 
      return(1);
    }
    Last edited by foniks munkee; 07-11-2002 at 07:55 PM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but i aint workin'
    There are several syntax errors that keep it from compiling. But past that your computations are off. Consider how one would simply calculate whether a number is prime:
    Code:
    /* Pseudocode: I hold no responsibility for errors syntactic, logical, or mathematical ;) */
    int main ( void )
    {
      int val, i, flag = 1;
      printf ( "Enter a number: " );
      if ( scanf ( "%d", &val ) == 1 ) {
        if ( val == 1 ) flag = 0;
        for ( i = 2; i < val; i++ )
          if ( i < val && val % i == 0 )
            flag = 0;
        printf ( "%d is %sprime\n", val, 
          flag != 1 ? "not " : "" );
      }
      return 0;
    }
    Loop through all values less than your value. If your value is divisible by any of them then it is not prime. This is a slower method, but it should work for what you need.

    -Prelude
    My best code is written with the delete key.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The algorithm in your function prime_number does not check if a number is prime, but it checks in a strange way if numbers are odd or even.

    Code:
    int prime_number ( int a)
    {
       int num2, tot;
       num2 = a/2;
       tot = a/num2;
       
         if ( tot == 2 )
            return 1;
         else 
            return 0;
    }
    What you do is, in theory, something like this:

    num2 = a / 2
    tot = a / num2 = a / (a / 2) = a * (2 / a) = 2a / a = 2

    In practice, there is a round-off error when dividing, so num2 is not always equal to a/2, but approximately a/2. In some cases a/num2 should give 2, but because of approximation, it will not always do.

    When designing algorithms, always use pen and paper to work them out on paper before coding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM