A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.
Example C Program to find whether a number is Prime or Composite Number
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 |
#include<stdio.h> int main() { int number, counter, notprime = 0; /*Ask the uesr to Enter a Positive Integer */ printf("Enter the positive integer to check for prime or composite\n"); scanf("%d",&number); /*Check the remainder for zero when you divide the numbers */ /* if remainder is zero then it means that the number is completely divisible */ /* by another another and hence it can not be prime*/ for(counter = 2; counter <= number/2; counter++){ if( (number % counter) == 0 ){ notprime = 1; break; } } /* notprime == 0 means it is a prime number */ if(notprime == 0){ printf("%d is a prime number",number); }else{ printf("%d is a composite number",number); } return 0; } |
Output:
