You are on page 1of 1

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

h> void main() { clrscr(); int n,i,np=0;//np is boolean operator (true/false) printf("\n Enter a number :"); scanf("%d",&n); for(i=2;i<=(n-1);i++) /*we know that a number which is divisible by 1 and divisible by itself Is A PRIME NUMBER. So no need to check n divisible by 1 and divisible by itself */ { if(n%i==0) { np=1; break; //come out of for loop } } if(np==1) // in if statement np=1 ,it confirms that number is composite. { printf("Sorry,its composite number %d",n); } else { printf("it is a prime number %d",n); }

OUTPUT: Enter a Number: 12 Sorry its a composite number 12 Enter a number 17 it is a prime number 17

Read more: http://wiki.answers.com/Q/How_to_find_if_a_number_is_prime_or_composite_with_C_progra mming#ixzz27ats08Mn

You might also like