C++ Program to check if number is prime or composite

Here we will write a C++ program to check whether the given number prime or composite number, we all know what is a Prime number and what is Composite number still, lets recall it quickly

Prime Number: A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).

Composite Number: A number that can be formed by multiplying together two smaller positive integers. Equivalently, it is a positive integer that has at least one divisor other than 1 and itself. For e.g. 14,15, 18 etc.

In short a number that is not prime is a composite number.

How program works:

  • This program takes in an integer num1 as a screen input from the user.
  • It then determines whether the integer is prime or composite.
  • It finally outputs the appropriate message by writing to the ‘cout’ stream.


using namespace std;

int main()
    int number,count=0;
    cout<<"Enter a number: ";
    for(int a=1;a<=number;a++)
        cout<<"\t"<<number<<" IS A PRIME NUMBER \n";
        cout<<"\t"<<number<<" IS A COMPOSITE NUMBER \n";
    return 0;



prime or composite