0

Prime or not error can just tell what mistake I have made in my code and I need to y flag is used for

#include <stdio.h> int main() { int num,i, flag=0; printf ("enter a number"); scanf("%d",&num); for(i=2;i<num/2;i++) { if(i%num==0){ flag=1; printf("not a prime number",num); break; } if(flag == 0) {       printf ("%d is a prime number",num);     } } return 0; } // CAN U CHECK THIS PROGRAM// IT DISPLAYS ALL AS PRIME NUMBER \\ AND NEED EXPLAIN ION Y FLAG IS USED

6th Jul 2020, 11:13 AM
Dilip Hamilton
2 odpowiedzi
+ 2
Dilip Hamilton Problem is in your print statement. flag is used to set value if condition is satisfied. It's a kind of decesion making to check whether number is prime or not. If number is not prime then we set flag 1. Now we can check later if flag is 0 then number is prime otherwise not. You can make flag boolean also.
6th Jul 2020, 11:34 AM
A͢J
A͢J - avatar
0
Can u tell what's the error here AJ Anant - C#/Java Challenger
6th Jul 2020, 11:38 AM
Dilip Hamilton