PRIME NUMBER PROGRAM IN C

In this example , you will learn a program to check whether a given number is prime number or not.


Prime number:
 

A number that is greater than 1 and can be only divided by 1 and itself, is called a prime number. In short, Prime numbers can't be divided by other numbers than itself and 1.


Example: 
2,3,5,7,11,13...... are the examples of prime numbers.

Program to check for prime number:


//check whether given number is prime or not

#include<stdio.h> 
int main( )
{
int num, i, n=0, flag=0;
printf("Enter the number :");
scanf("%d",&num);
n=num/2;

for(i=2;i<=n;i++)
{
if(num%i==0)
{
flag=1;
break;
}

if(flag==0)
printf("%d is prime number",num);
else
printf("%d is not prime number",num);

return 0;
}


Output:


Enter the number:5
5 is prime number


Enter the number:34
34 is not prime number



Note:

> 0 and 1 are not considered as prime numbers.
> 2 is the only even prime number.

__________________________________________________


Try this by your own! 
If you have any doubts feel free to drop a comment . I'll be happy to answer your questions in the comment section.
Keep coding.....


Comments

Post a Comment

Popular posts from this blog

DECIMAL TO OCTAL CONVERSION IN C

PROGRAM TO CONVERT BINARY CODE TO GRAY CODE

KRISHNMURTHY NUMBER PROGRAM IN C