Find Prime Number in C++

What is a PRIME NUMBER?

” A Natural number greater than 1 which has only two divisor 1 and itself is called prime number “.

For Example:  

5 is prime, because it has only two divisors 1 and itself.

C++ Program To Find Prime Numbers

  1. #include<iostream.h>
  2. #include<conio.h>
  3.         void main()
  4.         {
  5.          //clrscr();
  6.          int number,count=0;
  7. cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
  8.           cin>>number;
  9.            for(int a=1;a<=number;a++)
  10.                {
  11.                 if(number%a==0)
  12.                    {
  13.                   count++;
  14.                    }
  15.                }
  16.        if(count==2)
  17.          {
  18.           cout<<" PRIME NUMBER \n";
  19.          }
  20.        else
  21.          {
  22.           cout<<" NOT A PRIME NUMBER \n";
  23.          }
  24.        //getch();
  25.        }

    Output:
    C++ Programming prime number output sample

Leave a Reply

Your email address will not be published. Required fields are marked *