C++ program to find given number is prime or not using loops, recursion, and find prime numbers from 1 to n etc.
Method 1 : C++ program to find given number is prime or not
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int x1,k=0,num;
cout<<"Enter the number ";
cin>>num;
x1=sqrt(num);
for(int i=2;i<=x1;i++)
{
if(num%i==0)
k++;
}
if(k==0)
cout<<"Number is prime";
else
cout<<"Number is not prime";
return 0;
}
#include<math.h>
using namespace std;
int main()
{
int x1,k=0,num;
cout<<"Enter the number ";
cin>>num;
x1=sqrt(num);
for(int i=2;i<=x1;i++)
{
if(num%i==0)
k++;
}
if(k==0)
cout<<"Number is prime";
else
cout<<"Number is not prime";
return 0;
}
Method 2 : C++ program to find given number is prime or not using recursion
#include<iostream>
#include<math.h>
using namespace std;
int prime(int n,int i)
{
if(i==1)
return -1;
else if(n%i==0)
return 1;
prime(n,i-1);
}
int main()
{
int n,pow,result;
cout<<"Enter the number : ";
cin>>n;
result=prime(n,sqrt(n));
if(result==-1)
cout<<"Number is Prime";
else
cout<<"Number is not Prime";
}
#include<math.h>
using namespace std;
int prime(int n,int i)
{
if(i==1)
return -1;
else if(n%i==0)
return 1;
prime(n,i-1);
}
int main()
{
int n,pow,result;
cout<<"Enter the number : ";
cin>>n;
result=prime(n,sqrt(n));
if(result==-1)
cout<<"Number is Prime";
else
cout<<"Number is not Prime";
}
Method 1 : C++ program to find prime numbers from 1 to n
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int prime,n,x1,k=0,c;
cout<<"Enter the number ";
cin>>n;
cout<<"Prime numbers ";
for(int i=1;i<=n;i++)
{
x1=sqrt(i);
for(int y=2;y<=x1;y++)
{
if(i%y==0)
k++;
}
if(k==0)
cout<<i<<" ";
k=0;
}
return 0;
}
#include<math.h>
using namespace std;
int main()
{
int prime,n,x1,k=0,c;
cout<<"Enter the number ";
cin>>n;
cout<<"Prime numbers ";
for(int i=1;i<=n;i++)
{
x1=sqrt(i);
for(int y=2;y<=x1;y++)
{
if(i%y==0)
k++;
}
if(k==0)
cout<<i<<" ";
k=0;
}
return 0;
}
Method 2 : C++ program to find prime numbers from 1 to n using recursion
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int check(int n,int i)
{
if(i==1)
return -1;
else if(n%i==0)
return 1;
check(n,i-1);
}
void prime(int n)
{
if (n==0)
return ;
if(check(n,sqrt(n))==-1)
cout<<setw(4)<<n;
prime(n-1);
}
int main()
{
int n;
cout<<"Enter the number ";
cin>>n;
cout<<"Prime numbers :";
prime(n);
return 0;
}
#include<math.h>
#include<iomanip>
using namespace std;
int check(int n,int i)
{
if(i==1)
return -1;
else if(n%i==0)
return 1;
check(n,i-1);
}
void prime(int n)
{
if (n==0)
return ;
if(check(n,sqrt(n))==-1)
cout<<setw(4)<<n;
prime(n-1);
}
int main()
{
int n;
cout<<"Enter the number ";
cin>>n;
cout<<"Prime numbers :";
prime(n);
return 0;
}
C++ program to find sum of prime numbers
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int prime,n,x1,k=0,c,sum=0;
cout<<"Enter the number ";
cin>>n;
cout<<"Prime numbers ";
for(int i=1;i<=n;i++)
{
x1=sqrt(i);
for(int y=2;y<=x1;y++)
{
if(i%y==0)
k++;
}
if(k==0)
{
cout<<i<<" ";
sum+=i;
}
k=0;
}
cout<<endl<<"Sum = "<<sum<<endl<<endl;
return 0;
}
#include<math.h>
using namespace std;
int main()
{
int prime,n,x1,k=0,c,sum=0;
cout<<"Enter the number ";
cin>>n;
cout<<"Prime numbers ";
for(int i=1;i<=n;i++)
{
x1=sqrt(i);
for(int y=2;y<=x1;y++)
{
if(i%y==0)
k++;
}
if(k==0)
{
cout<<i<<" ";
sum+=i;
}
k=0;
}
cout<<endl<<"Sum = "<<sum<<endl<<endl;
return 0;
}
C++ program to print prime numbers in an array
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a[20],num,sqr,k=0;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements of array a1 : ";
for(int i=0;i<num;i++)
cin>>a[i];
cout<<"Prime numbers : ";
for(int i=0;i<num;i++)
{
sqr=sqrt(a[i]);
for(int y=2;y<=sqr;y++)
if(a[i]%y==0)
k=1;
if(k==1)
k=0;
else
cout<<a[i]<<" ";
}
return 0;
}
#include<math.h>
using namespace std;
int main()
{
int a[20],num,sqr,k=0;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements of array a1 : ";
for(int i=0;i<num;i++)
cin>>a[i];
cout<<"Prime numbers : ";
for(int i=0;i<num;i++)
{
sqr=sqrt(a[i]);
for(int y=2;y<=sqr;y++)
if(a[i]%y==0)
k=1;
if(k==1)
k=0;
else
cout<<a[i]<<" ";
}
return 0;
}
No comments:
Post a Comment