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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  	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"<<endl;
    
    else
        cout<<" El numero no es primo"<<endl;
        
        c = 2;
		delUno = 1;
        esPrimoFlag = 1;
        
    while(delUno < nume){
    	         
    	         
                while(c<=nume/2){
                
				if(delUno %c==0)
                esPrimoFlag=0;	
                
				
            c++;
			}
                
 		if(esPrimoFlag == 0)	
				
				cout<<" El numero "<<delUno<<" no es primo "<<endl;
				
				if(esPrimoFlag == 1)                
                cout<<" El numero "<<delUno<<" es primo "<<endl;
                
                 delUno++;                		
           }    
    }


Thanks in Advance (Y)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
number = 10;

for(i = 2; i<=number; i++)
{
prime=0;
for(x = 1; x<i; x++){
if(i%x==0){
prime++;
}
}
if(prime<=1){
it is Prime
}
else{
it is not Prime
}
}


hope this helps
lines 23 and 25
1
2
        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) {

should read:
 
    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.