Prime Number

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();
 }
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.
done but still the same problem
show me the code
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();
 }
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.
thank you its working!
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/195130/
Topic archived. No new replies allowed.