Prime Number
Jul 30, 2016 at 6:55am UTC
below is the code please check it out and tell me the mistake
it is prime number checker but every time it returns "prime" on every number please correct the mistakes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,b=0,flag=0;
printf("Enter any number:" ); scanf("%d" ,&a);
for (int c=a;c<a;c++)
{
b=a%c; if (b==0) { flag==1; }
}
if (flag==1)
{ printf("Not Prime" );
}
else
{ printf("Prime" );
}
getch();
}
Jul 30, 2016 at 7:10am UTC
Because u are giving C the same value as A on line 10 ( c == a), you loop will never actually be entered so flag will always stay 0.
Jul 30, 2016 at 7:18am UTC
done but still the same problem
Jul 30, 2016 at 7:19am UTC
show me the code
Jul 30, 2016 at 7:21am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,b=0,flag=0;
printf("Enter any number:" ); scanf("%d" ,&a);
for (int c=2;c<a;c++)
{
b=a%c; if (b==0) { flag==1; }
}
if (flag==0)
{ printf("Prime" );
}
else
{ printf("Not Prime" );
}
getch();
}
Jul 30, 2016 at 7:27am UTC
ohh yea on line 12 u have flag==1;
This actually does a comparision and returns bool value not assigns anything to flag so use =
instead.
Jul 30, 2016 at 7:29am UTC
thank you its working!
Jul 30, 2016 at 7:34am UTC
http://www.cplusplus.com/forum/beginner/195130/
Topic archived. No new replies allowed.