# Check whether Given Number is Prime Number or Not

“** Check whether Given Number is Prime Number or Not**” is again very basic problem of programming exercise. Here, we are given a number, entered by user and our task is to check whether entered number is prime number or not.

** Prime Number:** A number is said to be prime number if it is only divisible by 1 and by itself.

Examples of Prime numbers are 2,3,5,7,11,…

**NOTE: **1 is not a prime number.

**Example:**

Number = 7INPUT:7 is a prime number.OUTPUT:INPUT:Number = 99 is not a prime number.OUTPUT:

To check whether a number is prime number or not, we will check whether entered number is divisible from numbers 2 to (number-1). If any number from that range is divisible by entered number, then it is not a prime number. Else, it is a prime number.

**C++ Program to check whether a number is prime number or not is as follows: **

#include<bits/stdc++.h> using namespace std; int main() { int num; //Scan the number cout<<"Enter the number:"; cin>>num; bool res = true; /* We will check whether entered number is divisible by numbers from 2 to (num-1). If entered number is divisible by any number from 2 to (num-1), then it is not prime number. Else, it is prime number. */ for(int i = 2 ; i < num ; i++) { if(num % i == 0) { res = false; break; } } if(res == true && num!=1) /* 1 is not a prime number */ cout<<num<<" is a prime number"; else cout<<num<<" is not a prime number"; }

Enter the number: 11 11 is a prime numberOUTPUT: