 Open in App
Not now

# C++ Program To Check Whether a Number is Prime or not

• Last Updated : 17 Oct, 2022

Given a positive integer N. The task is to write a C++ program to check if the number is prime or not Definition

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}

The idea to solve this problem is to iterate through all the numbers starting from 2 to sqrt(N) using a for loop and for every number check if it divides N. If we find any number that divides, we return false. If we did not find any number between 2 and sqrt(N) which divides N then it means that N is prime and we will return True.
Why did we choose sqrt(N)?
The reason is that the smallest and greater than one factor of a number cannot be more than the sqrt of N. And we stop as soon as we find a factor. For example, if N is 49, the smallest factor is 7. For 15, smallest factor is 3.
Below is the C++ program to check if a number is prime:

## C++

 `// C++ program to check if a ` `// number is prime ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `n, i, flag = 1; ` ` `  `    ``// Ask user for input ` `    ``cout << ``"Enter a number: "``; ` ` `  `    ``// Store input number in a variable ` `    ``cin >> n; ` ` `  `    ``// Iterate from 2 to sqrt(n) ` `    ``for` `(i = 2; i <= ``sqrt``(n); i++) { ` ` `  `        ``// If n is divisible by any number between ` `        ``// 2 and n/2, it is not prime ` `        ``if` `(n % i == 0) { ` `            ``flag = 0; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``if` `(n <= 1) ` `        ``flag = 0; ` ` `  `    ``if` `(flag == 1) { ` `        ``cout << n << ``" is a prime number"``; ` `    ``} ` `    ``else` `{ ` `        ``cout << n << ``" is not a prime number"``; ` `    ``} ` ` `  `    ``return` `0; ` `} ` ` `  `// This code is contributed by shivanisinghss2110.`

Output

```Enter a number: 11
11 is a prime number```

Time Complexity: O(n1/2)
Auxiliary Space: O(1)

Method 2 : Optimized Approach using Wilsons theorem with O(N) complexity

If   ((n-1)! + 1) % n == 0  then  n is prime and else it is not prime

Example :

Input : 11
Output : 11 is a prime number
Explanation : (11-1)! + 1 = 3628801
3628801 % 11 = 0

Input : 8
Output : 8 is not prime number
Explanation : (8-1)! + 1 = 5041
5041 % 8 = 1

Implementation of the above approach :

## C++

 `// C++ program to check whether number is prime or not ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``// code ` `    ``int` `n = 11; ` `    ``int` `m = n - 1; ` `    ``int` `factm = 1; ` `    ``// find factorial of n-1 ` `    ``for` `(``int` `i = 1; i <= m; i++) { ` `        ``factm *= i; ` `    ``} ` ` `  `    ``// add 1 to (n-1)! ` `    ``int` `factn = factm + 1; ` `    ``if` `(factn % n == 0) { ` `        ``// if remainder is 0 ` `        ``cout << n << ``" is prime number"``; ` `    ``} ` `    ``else` `{ ` `        ``cout << n << ``" is not prime"``; ` `    ``} ` `    ``return` `0; ` `} ` `// this code is contributed by devendra solunke `

Output

` 11  is prime number`

Time Complexity: O(N) complexity only for calculating factorial  of (n-1) checking it is 0 or 1 using % takes constant time
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles