Sunday, 29 September 2013

Prime Number

A prime number is a natural number greater than 1 that has no positive divisor other than 1 and itself.


Print prime number upto a user given number. 


PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;

int main (){
  int num,i,n;
  bool prime; 
  cout<<"Prime no. upto:";
  cin>>num;
  for(i=3;i<=num;i++){
    prime = true;
    for(n=2;n<=i-1;n++){
      if(i%n == 0){
       prime = false;
       }
    }
    if(prime){
    cout<<i<<" is prime "<<endl;
    } 
   }
return 0;
}
Prime no. upto: 25
3
5
7
11
13
17
19
23

No comments:

Post a Comment