Sunday 15 March 2015

To check whether the given no. is prime or not Using C

# include <stdio.h>
# include <conio.h>

void main()
{
int i, n, flag = 0 ;
clrscr() ;
printf("Enter the Number : ") ;
scanf("%d", &n) ;
if(n <= 3)
flag = 0 ;
else
{
for(i = 2 ; i <= n / 2 ; i++)
if(n % i == 0)
{
flag = 1 ;
break ;
}
}
if(flag == 1)
printf("\nThe number is not prime") ;
else
printf("\nThe number is prime") ;
getch() ;
}


RUN 1 :
~~~~~~~
Enter the Number : 6
The number is not prime
RUN 2 :
~~~~~~~
Enter the Number : 11
The number is prime

No comments:

Post a Comment