+ 3
#include <iostream> #include <math.h> class Prime { int i=2; int flag=0; int x=2; int count=0; public: void calcPrime(int no) { while(x<=no) { while(i<=floor(sqrt(x))) /* Fastest method I know this comes from sieve of Eratosthenes method of finding prime numbers */ { if(x%i==0) { flag=1; break;} i++; } if(flag==0) { std::cout<<x<<"\n"; count++; } x++; i=2; flag=0; } } int getcount() { return count; } }; int main() { int no; Prime prime; std::cout<<"Enter number\n"; std::cin>>no; std::cout<<"Prime Numbers till "<<no<<" are\n"; prime.calcPrime(no); std::cout<<"So "<<prime.getcount()<<" Primes till "<<no; std::cout<<"\n !!!!!Please Like it!!!!!"; return 0; }
9th Nov 2016, 2:27 AM
Megatron
Megatron - avatar