Friday , December 15 2017
Home / C++ / Prime Number Program in C++

Prime Number Program in C++

(Last Updated On: October 11, 2017)

Prime Number Program in C++

Def :  The number that is divided by 1 or itself  and always greater than 1 . It can also be defined as the number which can are only divisional to 1. For example 2, 3, 5, 7, 11, 13, 17, 19, 23  are the prime numbers.

Here we will write a code which will detect whether the given number is prime or not.

 

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.   int a, i, b=0, flag=0;
  6.   cout << “Please Enter a Number\n”;
  7.   cin >> a;
  8.   b=a/2;
  9.   for(i = 2; i <= b; i++)
  10.   {
  11.       if(a % i == 0)
  12.       {
  13.           cout<<“Number is not Prime.”<<endl;
  14.           flag=1;
  15.           break;
  16.       }
  17.   }
  18.   if (flag==0)
  19.       cout << “Number is Prime.”<<endl;
  20.   return 0;
  21. }

About Admin

Leave a Reply

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