Prime Number Program In C++ - C++ Programming Tutorial
C++ Course / Graph Algorithms / Prime Number Program In C++

Prime Number Program In C++

BLUF: Mastering Prime Number Program In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Prime Number Program In C++

C++ is renowned for its efficiency. Learn how Prime Number Program In C++ enables low-level control and high-performance computing in the tutorial below.

A prime number is a numerical value exceeding 1 and divisible only by 1 or itself. In simpler terms, prime numbers are indivisible by any number except for 1 and itself. Illustratively, 2, 3, 5, 7, 11, 13, 17, 19, 23, and so forth, are instances of prime numbers.

Let's examine the C++ code for determining prime numbers. This program in C++ prompts the user to enter a number and then verifies if the input is a prime number.

Example

Example

#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:

Output

Enter the Number to check Prime: 17  

 Number is Prime.
Example

Enter the Number to check Prime: 57  

Number is not Prime.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience