Thread: Checking if an integer is a Prime number program too many prints

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    10

    Checking if an integer is a Prime number program too many prints

    Code:
    #include <stdio.h>
    Code:
    #include <math.h>
    
    
    int main(void)
    {
    	int n;
    	int x;
    printf("enter a number to check if it is prime\n");
    	scanf("%d",&x);	
        
    	
    	for (n=2;n<=sqrt(x);n++) {
    		if((x%n)==0)
    		{
    printf("%d not a prime number\n",x);
    		}
    	    else
    		{
    printf("%d prime number\n",x);
    		}
         } 
    }
    this program supposed to check if a given number prime or not,
    when I enter a value bigger than 9 it gives multiple prints, moreover if all those prints say "prime number" yes it is a prime number in real, but if at least one of says "not a prime number" it is not a prime number in real
    I tried many things it didn't work. I tried to find a similar code but I couldn't

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You have the printfs inside a loop.

    Also, you have to take into consideration precision, since n is an int and sqrt returns a double. Maybe a cast can provide a "fix"
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    try having a flag that starts out as 1 (true).
    int prime = 1;
    remove the printfs from your loop. instead if you find an instance of x%n == 0, set the flag to 0 (false) and break out of the loop (since one fail is enough to say not prime)
    then after the loop test your flag and print that it is either prime or not prime.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    after testing this, casting int as a double doesnt make a difference to it at all....surprising i know!!!

    BUT doing the
    Code:
    int prime=1;
    and using an IF to check if prime=0 to display "is prime" or "is not prime" works perfect!

    your code doesnt show anything for 1,2,or 3

    "new" code says
    1 is prime, 2 is prime 3 is prime....etc...

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by dmh2000 View Post
    try having a flag that starts out as 1 (true).
    int prime = 1;
    remove the printfs from your loop. instead if you find an instance of x%n == 0, set the flag to 0 (false) and break out of the loop (since one fail is enough to say not prime)
    then after the loop test your flag and print that it is either prime or not prime.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void) {
        int n;
        int i;
        int flag;
        
    printf("Enter value of N > ");
        scanf("%d", &n);
        flag = 1;
        for (i=2; (i<sqrt(n));i++) { 
            
            if ((n % i) == 0) 
                flag = 0;
            
                
        }
        
        if (flag=0)
    printf("%d is prime\n", n);
    else
    printf("%d is not prime\n", n);
    return0;
    }
    I came up with this but still not working

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I haven't analyzed your code, but a quick look-over brought this to my attention:

    Code:
    if (flag=0)
    You mean ==, not =

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    When I say cast I mean
    Code:
    for(.. (int) sqrt(i)...)
    Moreover, the for shouldn't have as condition the <=, rather the < ? Yes, it should

    Also, after this and the thing that Matticus said, you should be able to make it work. But, I would suggest you to think how inefficient this code is. I mean, if a guy from the Theoretical sector sees that, he is probably going to kill himself.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    curious why you changed x to i?

    also you changed your code from

    Code:
    for (n=2;n<=sqrt(x);n++) {
            if((x%n)==0)
    to

    Code:
        for (i=2; (i<sqrt(n));i++) { 
            
            if ((n % i) == 0)
    which changes your math, and what is devided by what

    other then that, your code matches mine (to a point!!!)

    //edit//
    ps although on some math formulas you need to cast ints to doubles (floats) for this you dont, and it will work

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    ok now for the probs!!!

    Code:
    for (i=2; (i<sqrt(n));i++) {
    should have been
    Code:
    for (i=2; (i<=sqrt(n));i++) {
    when you "fixed" it, you missed the =

    your

    Code:
    return0;
    should be
    Code:
    return 0;
    but your biggest mistake, is your flag
    Code:
        if (flag=0)
    you not only missed the second =

    but your flag would be backwards!!!

    flag of 1 says it IS a prime
    flag of 0 says it is not prime!!!

    Code:
        if (flag==1)
    not too bad for not doing it as long as most of us!

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    10

    finally it works

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void) {
        int n;
        int i;
        int flag;
        
    printf("Enter an integer to check if it is prime: ");
        scanf("%d", &n);
        flag = 1;
        for (i=2; (i<=(int)sqrt(n));i++) { 
            
            if ((n % i) == 0) 
                flag = 0;
        }
        
        if (flag==1)
    printf("%d is prime\n", n);
    else
    printf("%d is not prime\n", n);
    return0;
    }
    here it is finally working fine but I am still not sure how this flag works. I mean how copiler process this flag?
    Thanks to everybody who helped me out here!

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    ...but I am still not sure how this flag works.
    Take out a pen and paper. Pick out a value for 'n' and write it down. Then, by hand, step through each line of code, writing out all necessary values. Follow it through each step to see what your program is doing.

    It's good you got it to work, but it seems that's only the case because you received answers here. When you write a program yourself, you should fully understand what each and every line of code is doing. Take this advice if you really want to better understanding programming, and become proficient in it.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Ozlem Kayra View Post
    here it is finally working fine but I am still not sure how this flag works. I mean how copiler process this flag?
    That is precisely why I was hoping that you would be told to go away and think about it instead of being given the answer. This kind of logic problem is really simple and if you fail to come to the answer to it on your own then you're only going to fail further when it comes to moderately harder logic problems.
    Until you can solve this kind of problem on your own, you're not meant to be a programmer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Code:
    #include <stdio.h>
    Code:
    #include <math.h>
    
    
    int main(void) {
        int n;
        int i;
        int flag;
        
    printf("Enter an integer to check if it is prime: ");
        scanf("%d", &n);
        flag = 1;                               /* flag value is 1(true) now */
    for (i=2; (i<=(int)sqrt(n));i++) {      /* giving "i" integer values increasing one by one (i++)
                                                 starting from 2 to till equals to square root of "n"*/
            
    if ((n % i) == 0)                   /* divide n by for each "i" and check the remainder for each "i" and if any remainder 
                                                 equals to zero then flag is 0(false  */
                flag = 0;
        }
        
        if (flag==1)
    printf("%d is prime\n", n);         /*check flag for each "i" values if it is 1(true) and print n is "prime"*/
    else
    printf("%d is not prime\n", n);     /*all others ( if it is 0(false) print "not prime"*/
    return0;
    }
    I placed the comments next to lines, thats how I understood compiler do the process for this code pls tell me if I thought wrong

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You have to work on your code posting skills. Just copy your code as plain text to the text box and put code-tags around it.
    Your indentation is completley messed up too (which could be related to the above problem).

    Code:
    if (flag==1) 
        printf("%d is prime\n", n);         /*check flag for each "i" values if it is 1(true) and print n is "prime"*/ 
    else 
        printf("%d is not prime\n", n);     /*all others ( if it is 0(false) print "not prime"*/
    You don't check the flag for each "i". You check the flag after the loop whether any value of "i" has set it to 0.

    Just some additional comments:
    1) You calculate the square root for "n" at each iteration although "n" doesn't change. Therefore it's better to calculate it once before the loop and then just use the stored value.
    2) If your test fails when "i" is 2 (i.e. "n" is odd) all further even numbers (4, 6, 8, ...) will fail. Thus you should only loop through odd values for "i"
    3) As soon as you find a value for "i" which divides "n" there is no need to try other values. You can just break out of the loop.
    4) If you do break out prematurely there is really no need for a flag. Just test if "i" is equal to the square root of "n" after the loop to decide whether "n" is prime.

    Bye, Andreas

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by AndiPersti View Post
    You have to work on your code posting skills. Just copy your code as plain text to the text box and put code-tags around it.
    Your indentation is completley messed up too (which could be related to the above problem).

    Code:
    if (flag==1) 
        printf("%d is prime\n", n);         /*check flag for each "i" values if it is 1(true) and print n is "prime"*/ 
    else 
        printf("%d is not prime\n", n);     /*all others ( if it is 0(false) print "not prime"*/
    You don't check the flag for each "i". You check the flag after the loop whether any value of "i" has set it to 0.

    Just some additional comments:
    1) You calculate the square root for "n" at each iteration although "n" doesn't change. Therefore it's better to calculate it once before the loop and then just use the stored value.
    2) If your test fails when "i" is 2 (i.e. "n" is odd) all further even numbers (4, 6, 8, ...) will fail. Thus you should only loop through odd values for "i"
    3) As soon as you find a value for "i" which divides "n" there is no need to try other values. You can just break out of the loop.
    4) If you do break out prematurely there is really no need for a flag. Just test if "i" is equal to the square root of "n" after the loop to decide whether "n" is prime.

    Bye, Andreas
    Yea I know I will work on my indentations...
    Thanks for suggestions I think I can do top 3 easily, but I am not sure about breaking out prematurely I will try though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-27-2011, 11:25 AM
  2. checking if number is prime
    By jackson6612 in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2011, 03:02 PM
  3. Replies: 8
    Last Post: 12-30-2010, 10:08 PM
  4. Program the prints prime numbers
    By cloudstrife910 in forum C++ Programming
    Replies: 8
    Last Post: 09-22-2010, 03:03 PM
  5. Replies: 4
    Last Post: 01-29-2002, 02:42 AM

Tags for this Thread