Thread: Is it prime number or not

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    14

    Is it prime number or not

    Prompt user for an integer
    Read the integer into n

    IF n is less than 2
    Set prime to 0 ( 0 means false, i.e. n is not prime )
    ELSE
    Set prime to 1 ( 1 means true, i.e. assume n is prime )
    FOR values of i from 2 to n - 1 ( i represents all numbers smaller than n )
    IF n divided by i gives no remainder
    Set prime to 0 ( 0 means false, i.e. n is not prime )
    BREAK ( jump out from the loop )
    END IF
    END FOR
    END IF

    IF prime = 1
    Print message for prime number
    ELSE
    Print message for not prime number
    END IF

    I follow this algorithm but it's not working ...
    Code:
    #include <stdio.h>
    
    
    int main ()
    
    
    {
    
    
        int n, i, prime;
    
    
    
    
        printf("Enter a number : ");
        scanf("%d", &n);
    
    
        if (n<2)
        {
            prime = 0;
        }
        else
        {
            prime = 1;
            for (i=2;i<n;n--)
            {
                if (n%i==0)
                {
                    prime = 0;
                    break;
                }
            }
        }
    
    
        if (prime==1)
        {
            printf("Prime number\n", n);
        }
        else
        {
            printf("Not a prime number\n", n);
        }
    
    
        return 0;
    
    
    }
    Anyone help please?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Your for loop is wrong. You want to increment i and not decrement n. n should not change at all.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    14
    I see ... i changed it to i++; and it's working now
    but what is the meaning of FOR values of i from 2 to n - 1 ( i represents all numbers smaller than n ), i though that n-1 = n--?

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    You are modifying n, the number being checked for primeness, instead of incremening your counter ie "i".

    Code:
    for( i = 2; i < n; ++i){
            if( !( n % i ) ){
                    prime = 0;
                    break;
            {
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by CruelSoulz View Post
    I see ... i changed it to i++; and it's working now
    but what is the meaning of FOR values of i from 2 to n - 1 ( i represents all numbers smaller than n ), i though that n-1 = n--?
    i is your loop variable, it is what changes each time through the loop (you can think of it as i goes from 2 to n-1, from being a keyword from the pseudo code)
    2 is the starting value for i
    n is the number you wish to check if it is prime
    n - 1 is one less than that number, i.e. if you wish to check whether 13 is prime, n is 13 and n - 1 is 12

    n-- on the other hand mean "use the current value of n, then decrement it". For example, if n was 13, and you did x = n--; you would set x to the value of n (i.e. x = 13), then subtract one from n (i.e. n = n - 1)

    Hope that's clear.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by CruelSoulz View Post
    I see ... i changed it to i++; and it's working now
    but what is the meaning of FOR values of i from 2 to n - 1 ( i represents all numbers smaller than n ), i though that n-1 = n--?
    n is the number you are checking to see if it's prime. Throughout the test of lesser numbers, you want to change the current number being tested as a factor of n, but you do NOT want to change n, ever.

    BTW, you can stop testing numbers as a factor, when i > square root of n. Little math speed up for you.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    14
    Oh ... i get it , thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whether number is prime or not
    By amite in forum C Programming
    Replies: 22
    Last Post: 01-27-2011, 02:16 PM
  2. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 01:44 AM
  3. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  4. prime number.
    By tdoctasuess in forum C Programming
    Replies: 13
    Last Post: 05-13-2004, 08:03 AM
  5. Prime Number?
    By cprogramnewbie in forum C Programming
    Replies: 8
    Last Post: 03-16-2002, 12:18 AM