Program to print Prime Number, Upto N Prime Numbers, First N Primes Numbers, Prime Pattern in C++
//Is Number is Prime or Not.
//Prime Nos. upto N.
//First N Prime Nos.
//Prime Nos. Pattern
#include<iostream>
#include<iomanip>
using namespace std;
//Function Prototyping
int isPrime(int);
void uptoPrime(int);
void nPrimes(int);
void primePattern(int);
int main()
{
int n, ans;
cout<<"===============================================\n";
cout<<" USER DEFINED FUNCTIONS | PRIME NOS. \n";
cout<<"===============================================\n";
//No. is Prime Or Not.
cout<<"Enter Number to check, it is Prime or Not: ";
cin>>n;
ans=isPrime(n);
if(ans==-1)
cout<<"The Number "<<n<<" is not Prime Number\n";
else
cout<<"The Number "<<n<<" is Prime Number\n";
//Prime Numbers Upto N.
cout<<"\nEnter N to Print Prime Numbers Upto N: ";
cin>>n;
uptoPrime(n);
//First N Prime Numbers
cout<<"\nEnter N to Print Prime Numbers: ";
cin>>n;
nPrimes(n);
//Prime Pattern
cout<<"\nEnter N : ";
cin>>n;
primePattern(n);
cout<<"===============================================\n";
return 0;
}
int isPrime(int n)
{
for(int i=2;i<n;i++)
{
if(n%i==0)
return -1;
}
return 1;
}
void uptoPrime(int n)
{
int i, j, flag=0;
cout<<"The Prime Nos. from 1 to "<<n<<" are:\n";
for(i=2;i<=n;i++)
{
flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
flag++;
}
if(flag==0)
cout<<setw(4)<<i;
}
cout<<endl;
}
void nPrimes(int n)
{
int i, j, flag=0, cnt=0;
cout<<"The First "<<n<<" Prime Nos. are:\n";
for(i=2;cnt<=n;i++)
{
flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
flag++;
}
if(flag==0)
{
cnt++;
cout<<setw(4)<<i;
}
}
cout<<endl;
}
void primePattern(int n)
{
int i, j, temp=2;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;)
{
if(isPrime(temp)==1)
{
cout<<setw(4)<<temp;
j++;
}
temp++;
}
cout<<endl;
}
}
//Prime Nos. upto N.
//First N Prime Nos.
//Prime Nos. Pattern
#include<iostream>
#include<iomanip>
using namespace std;
//Function Prototyping
int isPrime(int);
void uptoPrime(int);
void nPrimes(int);
void primePattern(int);
int main()
{
int n, ans;
cout<<"===============================================\n";
cout<<" USER DEFINED FUNCTIONS | PRIME NOS. \n";
cout<<"===============================================\n";
//No. is Prime Or Not.
cout<<"Enter Number to check, it is Prime or Not: ";
cin>>n;
ans=isPrime(n);
if(ans==-1)
cout<<"The Number "<<n<<" is not Prime Number\n";
else
cout<<"The Number "<<n<<" is Prime Number\n";
//Prime Numbers Upto N.
cout<<"\nEnter N to Print Prime Numbers Upto N: ";
cin>>n;
uptoPrime(n);
//First N Prime Numbers
cout<<"\nEnter N to Print Prime Numbers: ";
cin>>n;
nPrimes(n);
//Prime Pattern
cout<<"\nEnter N : ";
cin>>n;
primePattern(n);
cout<<"===============================================\n";
return 0;
}
int isPrime(int n)
{
for(int i=2;i<n;i++)
{
if(n%i==0)
return -1;
}
return 1;
}
void uptoPrime(int n)
{
int i, j, flag=0;
cout<<"The Prime Nos. from 1 to "<<n<<" are:\n";
for(i=2;i<=n;i++)
{
flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
flag++;
}
if(flag==0)
cout<<setw(4)<<i;
}
cout<<endl;
}
void nPrimes(int n)
{
int i, j, flag=0, cnt=0;
cout<<"The First "<<n<<" Prime Nos. are:\n";
for(i=2;cnt<=n;i++)
{
flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
flag++;
}
if(flag==0)
{
cnt++;
cout<<setw(4)<<i;
}
}
cout<<endl;
}
void primePattern(int n)
{
int i, j, temp=2;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;)
{
if(isPrime(temp)==1)
{
cout<<setw(4)<<temp;
j++;
}
temp++;
}
cout<<endl;
}
}
No comments
Post your comments