Prime Numbers

A Programm To Print (n) Numbers Of Prime Numbers.


What is a Prime Number?
 A number that divdes only itself is called Prime Number.

Let us take a number 5.
            Try to divide the number 5 with less than 5.
We cannot divide it with any number without having a remainder.

Now let us think about prime number in your programm.

what actually we are going do is we are taking a number and try it to divide with a numbers which less than the number. If it divides the number without having a remainder except 0. 
        If it is zero we are flag it.and repate the loop until the remaining numbers complete their division. If any another gets remainder. Then it is not a prime number.

I am wiriting Programm for checking n is a prime or not.

#include <iostream>
using namespace std;

int main()
{  
int a = 37;
int count = 0;
// prime number starts from 2.
for(int i = 2; i < a; i++){
if(a % i == 0){
count++;
}
}
if(count == 0){
cout << "is prime" << endl;
}
else cout << "not prime" << endl;
return 0;
}

Output will be:-


Post a Comment (0)
Previous Post Next Post