I wrote a simple and short program to check for prime numbers in C. It is showing normal results for all numbers except for those numbers which have last digit as 5 by printing them as prime numbers, even those numbers which are clearly not prime, like 15, 25, 45 etc.
My question is that why is it doing so and how can I fix it?
Here is my code -:
#include<stdio.h>
int main()
{
int num, count=0;
printf("\nEnter a number : ");
scanf("%d",&num);
for(int i=2; i<=num/2; i++)
{
if(num%i==0)
count=1;
break;
}
if(count==1)
printf("\nNot a prime number.");
else
printf("\nPrime Number.");
}
Any help would be great.
Thank You.
>Solution :
#include<stdio.h>
int main()
{
int num, count=0;
printf("\nEnter a number : ");
scanf("%d",&num);
for(int i=2; i<=num/2; i++)
{
if(num%i==0){ // you have to add {} for multi-line code
count=1;
break;
}
}
if(count==1)
printf("\nNot a prime number.");
else
printf("\nPrime Number.");
}