Program to check prime no in C++
#include <iostream> // #include - preprocessor to include header files in the program
iostream is a header file that contains functions for input/output operations (cin and cout).
using namespace std; // for using standard variables name
int main(//function use for run the program and before main if using int then it will return integer value as return.
{
int n, notprime=0,prime=0; // create two variables(notprime and prime) and take equal to zero for ignore default value.
cout<<"enter number"; // cout- used to display output
cin>> n; // cin- used to accept the input
for( int i = 2; i<n; i++ ){ // take i=2 bcoz prime no start from 2 and
i<n ? mean if n=8 then condition execute till i<8 . if take i<=8 then it will not check prime no just repeat again and again it is not prime no . if(n%i==0)
{ // if condition is true then execute notprime=1
notprime = 1;
}
else { // if condition is false then execute prime =1
prime = 1;
}
}
if (notprime ==1){ // notprime==1 then it will show no is not prime.
cout<<"no is not prime";
}
else if(prime==1){ // prime==1 then it will show no is prime
cout<<"no is prime";
}
return 0;
}
note- after check this (i<n)condition once, i value will be incremented till i<n.
Comments
Post a Comment