# C++ Program to Check whether a number is a Prime number or not.

A Prime no is a whole number that is greater than 1 and only divisible by 1 and itself. 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29 are the first few prime numbers. Today I will show you how to write a program to check whether a number is a Prime number or not.Let’s see the code… C++ Program to Check whether a number is a Prime number or not.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include<iostream> using namespace std; int main() { int n, i; bool isPrime = true; cout << "Enter a positive integer: "; cin >> n; for(i = 2; i <= n / 2; i++) { if(n % i == 0) { isPrime = false; break; } } if (isPrime) cout << "This is a prime number"; else cout << "This is not a prime number"; return 0; } |

Output:

Read more