program-to-print-prime-number-in-cpp-2022-mymixindia.com

Program to Print Prime Number in Cpp 2022

What are the Prime Numbers in Maths?

The Primer Number are those numbers that are only divisible by themselves and by 1 also, and they leave no reminder. Also, the primer number is greater than one and they only have two factors themselves and 1. Example 13 is a prime number, 5 is a prime number.

There are 5 prime numbers under 12: 2, 3, 5, 7, 11.

The smallest prime number is 2, and the Largest prime number is 282,589,933 − 1

And the numbers which have more than 2 factors are composite numbers.

Here we are going to make a prime number logic in CPP.

Program to print prime number in CPP in 2022

//.........Mymixindia.com..........
//......Prime Number Program in C++.....
#include <iostream>  
using namespace std;  
int main()  
{  
  int num, i, a=0, flag=0;  
  cout << "Welcome to the Prime Number Program: \n";  
  cout << "Enter a Number to check Prime: ";  
  cin >> num;  //.....taking input from users......  
  a=num/2;  //......dividing the input number by 2 and storing that quotient in variable a.....
  for(i = 2; i <= a; i++)  
  {  
      if(num % i == 0)  //.......if the number is divided value of i which is incermenting in loop.....
      {  
          cout<<"Number is not Prime."<<endl;  //....message if the number is not a prime number.....
          flag=1;  //...setting the flag to 1....
          break;  
      }  
  }  
  if (flag==0){ //.....the flag is not 1 then the number is prime number means it have only two factors..... 
      cout << "Number is Prime."<<endl;  
  }
  return 0;  
}  

The output of the Program

Welcome to the Prime Number Program: 
Enter a Number to check Prime: 23    
Number is Prime.

Explanation of the above program

  1. Initializing the variables
  2. Printing the Message on the screen for introduction and for user input
  3. user input the number for checking that the given number is a prime number or not
  4. Then divide that number by 2 and store that quotient value in the variable ‘a’
  5. Then using for loop for the iteration for dividing that number with other numbers for checking that the number is having the 2 factors or more than 2 factors.
  6. Then in if condition for dividing and if it is true then the number is not a prime number and set the flag to 1(initially flag = 0).
  7. Then break to terminating the for-loop.
  8. Then another if condition which has the flag compared with zero then if it is true then the number is a prime number.

Program 2 – Making the same program with some changes.

Prime number program with the goto statement

//.........Mymixindia.com..........
//......Prime Number Program in C++ using goto statement.....
#include <iostream>  
using namespace std;  
int main()  
{  
  int num, i, a=0, flag=0;
  char choice = 'y';  
  cout << "Welcome to the Prime Number Program: \n"; 
  START: 
  cout << "Enter a Number to check Prime: ";  
  cin >> num;  //.....taking input from users......  
  a=num/2;  //......dividing the input number by 2 and storing that quotient in variable a.....
  for(i = 2; i <= a; i++)  
  {  
      if(num % i == 0)  //.......if the number is divided value of i which is incermenting in loop.....
      {  
          cout<<"Number is not Prime."<<endl;  //....message if the number is not a prime number.....
          flag=1;  //...setting the flag to 1....
          break;  
      }  
  }  
  if (flag==0){ //.....the flag is not 1 then the number is prime number means it have only two factors..... 
      cout << "Number is Prime."<<endl;  
  }
  cout<<"Do you want to continue Y/N \n"; //....ask user choice for continue the program or not...
  cin>>choice; //....user input the choice.....
  if(choice=='y'||choice=='Y') //.....if user input y or Y the program continue and the start again ....
  {
      goto START; //....it will jump to the START lable .....
  }
  else{
      cout<<"Thank-you\n"; //......if the user enter n or N or other input then the condition become false and thankyou will print...
  }

  return 0;  
}  

 

The output of the program

Welcome to the Prime Number Program:
Enter a Number to check Prime: 17
Number is Prime.
Do you want to continue Y/N
Y
Enter a Number to check Prime: 12
Number is not Prime.
Do you want to continue Y/N
y
Enter a Number to check Prime: 45
Number is not Prime.
Do you want to continue Y/N
y
Enter a Number to check Prime: 100
Number is not Prime.
Do you want to continue Y/N
y
Enter a Number to check Prime: 33
Number is not Prime.
Do you want to continue Y/N
n
Thank-you

These are the 2 programs of Prime Number.

Related posts:

5 Namespace Program in C++ 2022

Constructors Program in Java

Java Data Type Program

HTML Tutorials 

CSS tutorials