Programming Examples

Cpp program to check whether it is a prime number or not using for loop


Write a cpp code to input a number and check whether it is a prime number or not.

Solution

#include<iostream>
using namespace std;
int main(){
     int num,a;
     bool isPrime = true;
     cout<<"Enter any positive integer number";
     cin>>num;
	if(num==1){
	cout<	}
     for(a=2;a<=num/2;a++){
        if(num%a==0)
        {
            isPrime=false;
            break;
        }
     }
     if (isPrime) {
        cout << num << " is a prime number" << endl;
    } else {
       cout << num << " is not a prime number" << endl;
    }
     return 0; 
}
Output

Output:

Enter any positive integer number 12

12 is not a prime number

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz