Prime Number: It is a natural number other than $1$ which has only two divisors, $1$ and itself.
Examples:
$3$ is a prime number. It has two divisors $1$ and $3$ itself.
$13$ is a prime number. It has two divisors $1$ and $13$ itself.
Algorithm:
Steps:- Take an integer to check whether it is prime or not.
- Divide the integer by the all the numbers from 1 to till that number
- Count the number of times the integer is divisible
- If the count is 2 then it is a prime number
- Otherwise it is not a prime number.
#include "iostream" #include "conio.h" using namespace std; int main() { int n, count=0; cout<< "Enter a number to check prime : "; cin>>n; for(int a=1;a<=number; a++) { if(number%a==0) { count++; } } if(count==2) { cout<<" PRIME NUMBER \n"; } else { cout<<" NOT A PRIME NUMBER \n"; } return (0); }
INPUT: Enter a number to check prime : $7$
OUTPUT: PRIME NUMBER
INPUT: Enter a number to check prime : $15$
INPUT: Enter a number to check prime : $15$
OUTPUT: NOT A PRIME NUMBER
No comments:
Post a Comment