Prime Numbers Upto N



#include<iostream>
#include<iomanip>

using namespace std;

bool isPrime(int n)
{
if (n==1) return false;
else
{
for(int i=2;i<n;i++){
if(n%i==0)
return false;
}
}
return true;
}

int main()
{
int i, n, cnt=0;
cout<<"=====================\n";
cout<<"PRIME NUMBERS UPTO N\n";
cout<<"=====================\n";
cout<<"Enter N: ";
cin>>n;
cout<<"The Prime Numbers from 1 to "<<n<<" are:\n";
for(i=1;i<=n;i++)
{
if (isPrime(i))
{
cout<<setw(8)<<i;
cnt++;
}
}
cout<<endl;
cout<<"Total "<<cnt<<" Prime Nos. are between 1 to "<<n<<endl;
}


Click to Watch Video

No comments

Post your comments

Powered by Blogger.