C++ Program to Check the Number is Prime Number Or Not

 C++ Program to Check the Number is Prime Number Or Not


Example to check whether an integer (entered by the user) is a prime number or not using for loop and if...else statement.
Prime numbers are such numbers which are divisible by 1 and itself only.

you can also watch the video explained in hindi : https://youtu.be/LL50cpduslU

Here is the source code : 
#include<iostream>
using namespace std;

int main(){
    int n,i;
    cin>>n;
    for(i=2;i<n;i++){
        if(n%i==0)
        break;
    }
    if(i==n)
    cout<<"prime"<<endl;
    else
    cout<<"non prime"<<endl;

    return 0;
}

Some practice questions of similar difficulty :
1.Wap to check whether the number is even or not?
2.Wap to find the sum of n natural numbers takin n from user.
3.Wap to find the reverse of the number.
4.Wap to check the character is consonant, vowel or special characters.
5.Wap to print the all prime number between the given range.
Previous
Next Post »