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, but the first one was the one my friend wrote, and 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……
If we’re talking about the first program, there is a problem with how you read the integer n. You need to pass its address:
I’m surprised that your compiler doesn’t catch this.