top of page
Click here to go to the home page of AskTheCode.

Write a C++ Program to Check Whether a Number is Prime or Not | AskTheCode

Team ATC

Asked by: Umar Khan | Category: C++ ( c plus plus ) | Program to check a Prime number

 

#include <iostream>

using namespace std;

int main() {

int n, flag=0;

cout<< "Enter the number to check whether it's Prime or not: ";

cin>> n;

if(n==1 || n==0){

cout<< "Neither Prime nor Composite";

}

else{

for(int i=2; i<n; i++){

if(n%i==0){

flag=1;

}

}

if(flag==1){

cout<< "Not a Prime number";

}

else{

cout<< "Prime number";

}

}

return 0;

}

Recent Posts

See All

Comments


bottom of page