This c++ program checks if a number is a prime or not. A number which is greater than 1 and can only be divided by 1 and itself are prime numbers. These prime numbers include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 and so on. A FOR loop is implemented in this program. If the number entered by the user can be divided by another number that is within its range to give zero as the remainder then, this number is not a prime number.
#include <iostream.h> #include <math.h> void main() { int num, a=0; cout<<"enter a number: "; cin>>num; for(int i=2;i<num;i++)//loop begins at 2 since 1 can divide any number { if(num%i==0) { a++; } } if(a==0) { cout<<num<<" is a prime number"; } else cout<<num<<"is not a prime"; }