C LAB‎ > ‎(Sem1) Introduction to C‎ > ‎

WAP to check if number is a prime or not.

posted Oct 30, 2010, 2:38 AM by Neil Mathew   [ updated Nov 6, 2010, 4:44 AM ]

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
 
int main()
{
 
int num;
 
printf("\n Enter the number to check whether prime or not: ");
scanf(" %d ",&num);
 
if( num==0 || num==1 )
{
printf("\n The number is not prime. ");
}
else
{
 
int i=0; 
int flag=1; // 1 if prime, 0 if not prime
 
for(i=2; i<num; i++)
{
 
 if(num%i==0)
 {
 flag=0;
 break;
 }
 
}
 
if(flag==1)
{
printf("\n The number is prime. ");
}
else
{
printf("\n The number is not prime ");
}
 
}
 
return 0;
}

OUTPUT:

 Enter the number to check whether prime or not: 1

 The number is not prime. 

 Enter the number to check whether prime or not: 5

 The number is prime. 

 Enter the number to check whether prime or not: 12

 The number is not prime 
Comments