Write a program to check given number is prime number or not

Write a program to check given number is prime number or not


Default
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
// Write a program to check given number is prime number or not
// Eg: 2 -> Prime, 4-> Not prime

#include
int main()
{
int inp,i,count=0;
printf("Enter a number:");
scanf("%d",&inp); // input from user
if (inp<2)
printf("Given number is not Prime number\n");
else
{
for (i=2;i<=inp/2;i++)
{
if(inp%i==0) // checking for factors
{
count=1;
break;
}
}
 
if(count==0) // prime if no factors
printf("Given number is Prime number\n");
else // not prime if any factor other than 1 and itself
printf("Given number is not Prime number\n");
   }
return 0;
}

No comments:

Post a Comment