Prime Number Program in C++

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this Basic C++ Program, we will have a look at how to write a c++ program to check the prime number.

A prime number is a number that is greater than 1 and is divided by 1 or by itself. In other words, prime numbers can not be divided by other numbers other than itself or 1. For example, 2, 3, 5, 7, 11, 13, 17, 19, 23 … are the prime numbers.

Let’s see the program of prime numbers in C ++. 

C++ Program of Prime Number

#include <iostream>  

using namespace std;  

int main()  

{  

  int n, i, m=0, flag=0;  

  cout << "Please enter the Number to check Prime: ";  

  cin >> n;  

  m=n/2;  

  for(i = 2; i <= m; i++)  

  {  

      if(n % i == 0)  

      {  

          cout<<"Provided Number is not Prime."<<endl;  

          flag=1;  

          break;  

      }  

  }  

  if (flag==0)  

      cout << "Provided Number is Prime."<<endl;  

  return 0;  

}  

Output:

Please enter the Number to check Prime: 17  

Provided Number is Prime.   


Please enter the Number to check Prime: 57  

Provided number is not Prime.


 

Leave a Comment

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