Need help with prime number C program.

This is a discussion on Need help with prime number C program. within the C Programming forums, part of the General Programming Boards category; I'm working on a program for a class: Write a program that asks for an integer from the user and ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    10

    Need help with prime number C program.

    I'm working on a program for a class:

    Write a program that asks for an integer from the user and determines whether or not the integer is odd or even, then determines if that number is a prime number.

    I have it working almost perfectly, but the problem I'm finding is that it says that 1 and 4 are prime numbers. All the other numbers i check are correct except 1 and 4. Does anyone know why this is?

    Here is the code I have so far:

    Code:
    #include <stdio.h>
    int main ( void )
    {
        int integer;
        int x;
        
        printf( "Enter an integer (-1 to end): " );
        scanf( "%d", &integer );
    
        while( integer != -1 ) 
        {
    
            if( integer % 2 == 0 ) 
            {
                printf( "The number you have entered is an even number.\n" );
            }
            else 
            {
                printf( "The number you have entered is an odd number.\n" );
            }
            
            for( x = 2; x < integer / 2; x++ )
            {
                if( integer % x == 0 )
                {
                    printf( "The number you have entered is not a prime number.\n" );
                    break;
                }
            }
    
            if( !( x < integer / 2 ))
            {
                printf( "The number you have entered is a prime number.\n" );
            }
            
            printf( "Enter an integer (-1 to end): " );
            scanf( "%d", &integer );
        
        }
        
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    I would try instead of "integer / 2" to use "((integer / 2) + 1)"

    Should fix the four issue; not sure about 1.

    Edit1: Or you might try "<=" instead "<"; I prefer not to use "<=" except when that is more correct.
    In this case, I am not sure of the more correct choice. I might use <= or the plus 1 solution; both seems valid.

    Edit2: For 1 I would just say anything smaller than 2 is not prime; so, I suggest adding an if statement doing that.

    Tim S.
    Last edited by stahta01; 10-19-2012 at 01:23 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Quote Originally Posted by stahta01 View Post

    Or you might try <= instead <.

    Tim S.
    If you let x = integer at any time in the for loop, (x % integer) will always output zero. So the program would output that no number is prime. The < is fine.

    With input 1, you would not have gone through the for loop and would therefore have
    Code:
    if (!(2 < (1/2))
    The requirement for if is satisfied, it will output that it is prime.
    Code:
    if (!(x < integer/2) && (integer != 1))
    ^this would fix it from outputting that it is prime.

    Adding 1 to (integer / 2) doesn't fix four, fyi. I will work on that later when I have time.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Brian29, have you learnt how to write your own functions? It would be simpler to write a function, let's call it is_prime, that returns 1 if the argument is prime, or 0 otherwise. This way, you don't need that extra check: you can just return 0 once a factor is found, and at the end of the function return 1 (though you may need an extra check for integers less than 2).

    Quote Originally Posted by Warofwest
    If you let x = integer at any time in the for loop, (x % integer) will always output zero. So the program would output that no number is prime. The < is fine.
    You misread stahta01's suggestion: we're talking about x <= integer / 2, not x <= integer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    Quote Originally Posted by stahta01 View Post
    I would try instead of "integer / 2" to use "((integer / 2) + 1)"
    This actually worked to fix the 4 problem, thank you.

    We have learned to write functions but I'm not very clear on how do it. I'm taking this class online and have had to learn most on my own.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime number program
    By jigamuffin in forum C Programming
    Replies: 5
    Last Post: 03-11-2012, 07:49 PM
  2. Prime number program help?
    By TechnoKid8 in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2009, 07:02 PM
  3. Prime number program
    By SVXX in forum C++ Programming
    Replies: 8
    Last Post: 10-03-2008, 11:31 AM
  4. Replies: 3
    Last Post: 03-29-2005, 03:24 PM
  5. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21