In this example, you will learn how to check whether a number is prime or not
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include<iostream> #include<stdlib.h> using namespace std; int main() { int nb,r=0; cout<<"Enter a number : ";cin>>nb; for(int i = 1 ; i <= nb ; i++ ) { if(nb % i == 0) { r++; } } if(r>2) cout<<nb<<" is not a prime number"<<endl; else cout<<nb<<" is a prime number"<<endl; system("pause"); } |
Output:
In this example, you have learned how to check whether a number is prime or not