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

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;

}

37 views0 comments

Recent Posts

See All

Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes