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