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

This is a discussion on Checking if an integer is a Prime number program too many prints within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> Code: #include <math.h> int main( void ) { int n; int x; printf ( "enter a number ...

1. ## 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. 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"

3. 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. 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. Originally Posted by dmh2000
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. I haven't analyzed your code, but a quick look-over brought this to my attention:

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

7. 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.

8. 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. 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;`
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. ## 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. ...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. Originally Posted by Ozlem Kayra
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.

13. 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. 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.

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. Originally Posted by AndiPersti
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.