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

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 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 Best Website and Software Company in Allahabad