Prime Number Program in C++ – [ Program with Explanation ]

 

Prime Number Algorithm:

1st Step: START.

2nd Step: Enter the number to Check for prime.

3rd Step: if the number is divisible by any other number and also divisible by itself then print “Number is Prime Number”.

4th Step: Else print “Number is not a Prime Number”.

5th Step: STOP.

1. Prime Number Program in C++

In this program, the compiler will ask the user to enter the number which user wants to check whether the number is prime or not. And after checking the compiler will print the output on the screen.

C++

Output:

prime number program in c++
prime number program in c++

Explanation:

let us consider n=5 for loop becomes for(i=1; i<5; i++)

1st Iteration: for(i=1; i<=5; i++) in 1st iteration i is incremented i.e the value of i for the next iteration is 2.If (n%i==0) then c is incremented i.e (5%1==0) then c is incremented.Here, (5%1=0) so c is incremented i.e c=1.

2nd Iteration: for(i=2;i<=5;i++) in 2nd iteration i is incremented i.e the value of i for the next iteration is 3.If (n%i==0) then c is incremented i.e (5%2==0) then c is incremented.here,(5%2!=0) so c is not incremented i.e c=1.

3rd Iteration: for(i=3;i<=5;i++) in 3nd iteration i is incremented i.e the value of i for the next iteration is 4.If (n%i==0) then c is incremented i.e (5%3==0) then c is incremented.Here,(5%3!=0) so c is not incremented i.e c=1.

4th Iteration: for(i=4;i<=5;i++) in 4th iteration i is incremented i.e the value of i for the next iteration is 5.If (n%i==0) then c is incremented i.e (5%4==0) then c is incremented.Here,(5%4!=0) so c is not incremented i.e c=1.

5th Iteration: for(i=5;i<=5;i++) in 5th iteration i is incremented i.e the value of i for the next iteration is 6.If (n%i==0) then c is incremented i.e (5%5==1) then c is incremented.Here,(5%5=0) so c is incremented i.e c=2.

6th Iteration: for(i=6;i<=5;i++) in 6th iteration the value of i is 6 which is greater than n i.e i>n(6>5) so the for loop is terminated.Here c=2 so the given number is prime number.

Time Complexity of This solution is O(n).

2. Program for Prime Number in C++ Using While Loop

In this program, we will use while loop instead of for loop. But, the logic behind the program in same.

C++

Output:

using while loop
using while loop

3. Optimized School Method in C++

C++

Output:

prime number program in c++ using second method

4. C++ Program to Print Next Prime Number

In this program when the user enters any number to check whether it is prime or not then the compiler will print the next prime number occurs after the entered number by the user.

C++

Output:

next prime number

In the above program, we have entered the number as 5. And the compiler printed 7 as next prime number.

5. C++ Program to Print Prime Numbers from 1 to 100

In this program, we will print all the prime numbers between 1 and 100.

C++

Output:

prime number from 1 to 100

6. C++ Program to print all Prime Numbers Between 1 to n

After Executing this program the compiler will ask the user to enter the number n to print all prime numbers between 1 to n. And then the compiler will start printing all the prime numbers.

C++

Output:

prime number from 1 to n

No comments