### Need help with Prime numbers

Hello all, sorry for using the same title as everyone.

There are some words in the code that are spanish, sorry D:

In summary, what I'm trying to do is when the user enters a number(lets say a 10)

The program must say the numbers through 1 to 10 and tell each of them if they are Prime or Not.

But, when I run, the output of the number through 1 to 9 the program always say they are Prime

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` `````` int verPrimo(int num) { int nume,delUno,c, c2, sum, esPrimoFlag; cout<<" Entra un numero - >"; cin>>nume; c2 = 2; esPrimoFlag=1; while(c2<=nume/2){ if(nume %c2==0) esPrimoFlag=0; c2++; } if(esPrimoFlag==1) cout<<" El numero es primo"<

 ``1234567891011121314151617`` ``````number = 10; for(i = 2; i<=number; i++) { prime=0; for(x = 1; x

hope this helps
lines 23 and 25
 ``12`` `````` c = 2; esPrimoFlag = 1;``````

Should be inside the loop, move them to around line 29, so they are reset for each iteration.

Line 30
 `` `` `` while (c<=nume/2) {``

 `` `` `` while (c<=delUno/2) {``

Also, 1 is not prime, by definition, so you may need to make a special case for that (an extra `if` statement somewhere).
Last edited on
Thanks, both of you, i can now sleep in peace.

Btw, both solutions solved the problem.

Good night to both of you :3

Topic archived. No new replies allowed.