C Program to Check Prime or Composite Number

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.

This video tutorial teaches you to how to write a program in c programming language to find or check whether a entered number is prime number or composite number.

In this tutorial we are gonna ask the user to enter the number to check for prime or composite, then we check that number and then we print out whether the entered number is prime number or composite number.


Source code for this video tutorial

#include <stdio.h>

int main()
{
 int number, counter, isprime = 0;
 printf("Enter the positive integer to check for prime or composite\n");
 scanf("%d",&number);

 for(counter = 2; counter <= number/2; counter++){

  if( (number % counter) == 0 ){
   isprime = 1;
   break;
  }

 }

 if(isprime == 0){
  printf("%d is a prime number",number);
 }else{
  printf("%d is a composite number",number);
 }

 return 0;
}



To share your thoughts, Join the Discussion on Google+

Back To Top