Prime or Composite Number - C++ Program

Q. Write a C++ program to check whether the number is prime or not.

Answer:

Prime number is number greater than 1. It can be divided without a remainder, only by itself and by 1. 0 and 1 are not considered as prime numbers.

#include<iostream>
using namespace std;
int main()
{
        int num, i, count=0;

        cout<<"\n Enter Number : ";
        cin>>num;

        for(i=2;i<num;i++)
        {
                if(num%i==0)
                {
                        count++;
                        break;
                }
        }
        if(count==0)
        {
                cout<<"\n Prime Number";
        }
        else
        {
                cout<<" \n Not a Prime Number";
        }        
        return 0;
}


Output:

prime no