How to Find Prime Number in C++
What is a PRIME NUMBERPrime Number is a Natural number which is greater than 1 and it has only two divisor 1 and itself is Called Prime number ".
Also Read: How to write C++ program to find Fibonacci Series
C++ Program To Find Prime Number#include<iostream.h>
cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
cout<<" PRIME NUMBER \n";
cout<<" NOT A PRIME NUMBER \n";
Prime Number C++ Program Logic
To check that how much times it has divided to numbers from 1 to itself we take a variable and increment it each times when a number is divided.
Also Read: How to write C++ Program to find factorial of Number
As You may already Know in C++ Programming we take a FOR LOOP which will start from 1 up to number that has entered to check whether it is PRIME NUMBER or not with in FOR LOOP we will set an IF condition and place counter (count++) variable in its body so whenever a number from 1 to number which has entered to check divides than IF condition becomes true and counter variable will be incremented.
When FOR LOOP is completed we check from IF condition that:
- if counter variables value is equal to 2 than number is PRIME
- else NUMBER IS NOT PRIME.
Example Code to Find Prime Number
|Example Code to Find Prime Number in C++|