prime number

Feb 6, 2016 at 7:24am
I was trying to do prime number detector but when I entered "2" it didn't give me a outprint. And when I entered multiples of prime numbers (25, 9..), it printed 'prime number'. Also how can I process 10 and more digited numbers? Thanks.

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
  #include <stdio.h>

int number,i;

void f()
{
	if(number<=1)
	{
		printf("number is not prime \n");
	}
	
	
	else 
	{
		for(i=2;i<number;i++)
	{
		
		if(number%i==0)
		{
			printf("number is not prime \n"); break;
		}
		
		
		else
		{
			printf("number is prime \n"); break;
		}
		
	}
		
		
				  	
	}	
}

int main()
{
	while(915) 
	{
	scanf("%d",&number);
	f();
	
	}
}	
Feb 6, 2016 at 7:54am
Your problem is here:
for(i=2;i<number;i++)
i = 2
number = 2
2 is not less than 2
the for loop never executes.
Feb 6, 2016 at 7:58am
thanks "2" problem solved but still '9,25..' looks prime.
Feb 6, 2016 at 8:18am
Oh yeah, because it's an if else statement, so the for loop never executes more than once. It compares the number against 2 and if it fails that first if condition, it must be prime and breaks. Make your 'if else' an 'if else if' and you should be good.
Also, you need another condition to your if statements. You'll need to check in number equals i also.
Topic archived. No new replies allowed.