How to Write C++ Program to Find Prime Number | Virtual Study Solutions

Adsetra Ads

 

How to Find Prime Number in C++


Welcome to Virtual Study Solutions C++ Tutorial. Today we will discuss and learn how to Find Prime Number in C++ Programming.
Topics we will cover in this tutorial will be as follows:
  1. What is a Prime Number
  2. C++ Program to Find Prime Number
  3. Prime Number C++ Program Logic
  4. Example Code to Find Prime Number
How to Write C++ Program to Find Prime Number
 C++ Program to Find Prime Number

What is a PRIME NUMBER

Prime Number is a Natural number which is greater than 1 and it has only two divisor 1 and itself is Called Prime number ".
Lets take a Example:
2, 3, 5, 7 is prime Number, because it has only two divisors 1 and itself.

Also Read: How to write C++ program to find Fibonacci Series

C++ Program To Find Prime Number

#include<iostream.h>

#include<conio.h>

void main()

{

//clrscr();

int number,count=0;

cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";

cin>>number;

for(int a=1;a<=number;a++)

{

if(number%a==0)

{

count++;

}

}

if(count==2)

{

cout<<" PRIME NUMBER \n";

}

else

{

cout<<" NOT A PRIME NUMBER \n";

}

//getch();

}

Prime Number C++ Program Logic

To Understand the C++ Program logic to find Prime number first of all Recall PRIME NUMBER definition which is " a number which divides by 1 and itself ".

So we in our program we will make a condition like that in which user will enter a number and our program will check it by dividing it from 1 up to itself.

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.
Because if number divided two times by 1 and itself counter variable will incremented two times if more than two times counter variable will have value greater than 2. 

Also Read: What is Recursive function in c++ with Examples

Example Code to Find Prime Number

Here is the Example Code of C++ Program to Find Prime Number Screenshot:


Example Code to Find Prime Number in C++ Programming
Example Code to Find Prime Number in C++ 

Code output to Find Prime Number

Here is the Sample Output of Example program to find Prime Number:


Sample Output of Example program to find Prime Number
Sample Output of Example program to find Prime Number

We Recommended You to 
change the logic of above Program For fast learning and do experiments with codes.

Post a Comment

 

Top