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.
#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();
}
}
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.