Prime Numbers in C?
- by Ali Azam Rana
FIRST PROGRAM
#include<stdio.h>
void main()
{
int n,c;
printf("enter a numb");
scanf("%i",n);
for(c=2;c<=n;c++)
{
if(n%c==0)
break;
}
if(c==n)
printf("\nprime\n");
else
printf("\nnot prime\n");
getchar();
}
SECOND PROGRAM
#include <stdio.h>
int main()
{
printf("Enter a Number\n");
int in,loop,rem,chk;
scanf("%d",&in);
for (loop = 1; loop <=in; loop++)
{
rem = in % loop;
if(rem == 0)
chk = chk +1;
}
if (chk == 2)
printf("\nPRIME NUM ENTERED\n");
else
printf("\nNUM ENTERED NOT PRIME\n");
getchar();
}
the 2nd program works other was the one my friend wrote the program looks fine but on checking it by stepping into we found that the if condition in first program is coming true under every input so whats the logical error here please help me found out......