Prime Number or Composite Number Example Program in C Programming Language

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 #include<stdio.h>
 2 int main()
 3 {
 4  int number, counter, notprime = 0;
 5  
 6  /*Ask the uesr to Enter a Positive Integer */
 7  printf("Enter the positive integer to check for prime or composite\n");
 8  scanf("%d",&number);
 9 
10  /*Check the remainder for zero when you divide the numbers */
11  /* if remainder is zero then it means that the number is completely divisible */
12  /* by another another and hence it can not be prime*/
13  for(counter = 2; counter <= number/2; counter++){
14   if( (number % counter) == 0 ){
15    notprime = 1;
16    break;
17   }
18  }
19 
20  /* notprime == 0 means it is a prime number */
21  if(notprime == 0){
22   printf("%d is a prime number",number);
23  }else{
24   printf("%d is a composite number",number);
25  }
26  return 0;
27 }