Home › Tutorials & Guides › Coding Tutorials › Prime Number Program in C++

Prime Number Program in C++

This article guide you for ā€œhow to write a Prime Number Program in C++ā€. Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can’t be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17, 19, 23…. are the prime numbers.

Let’s see the prime number program in C++. In this C++ program, we will take an input from the user and check whether the number is prime or not.

Palindrome program in C++

    #include <iostream>  
    using namespace std;  
    int main()  
    {  
      int n, i, m=0, flag=0;  
      cout << "Enter the Number to check Prime: ";  
      cin >> n;  
      m=n/2;  
      for(i = 2; i <= m; i++)  
      {  
          if(n % i == 0)  
          {  
              cout<<"Number is not Prime."<<endl;  
              flag=1;  
              break;  
          }  
      }  
      if (flag==0)  
          cout << "Number is Prime."<<endl;  
      return 0;  
    }  

Output:

Enter the Number to check Prime: 19 
Number is Prime.
Enter the Number to check Prime: 55
Number is not Prime.
Note: If you interested lo learn C++ through web, You can click here

Conclusion:

Hi guys, I can hope that you can know that how to make Prime Number Program. If you like this post as well as know something new so share this article in your social media accounts. If you have any doubt related to this post then you ask in comment section.

Similar Posts

Leave a Reply

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