C++ Program To Check Whether A Number Is
The Sum Of 2 Prime Numbers
#include <iostream>
using namespace std;
int check_prime(int n);
int main() {
int n, i, flag=0;
cout << "Enter A Positive
Integer: ";
cin >> n;
for(i=2; i<=n/2; ++i) {
if (check_prime(i)==0){
if ( check_prime(n-i)==0) {
cout << endl<< n <<
" = " << i << " + " << n-i << endl;
flag=1;
}}}
if (flag==0)
cout << endl << n <<
" Can't Be Expressed As Sum Of Two Prime Numbers." << endl;
return 0;
}
int check_prime(int n) /*check prime
number*/ {
int i, flag=0;
for(i=2;i<=n/2;++i) {
if(n%i==0) {
flag=1;
break;
}}
return flag;
}
#include <iostream>
using namespace std;
int main() {
int num1, num2, count, n;
cout << "Enter First Number:
";
cin >> num1;
cout << "Enter Second Number:
";
cin >> num2;
while (num1>=num2 || num1<1 ||
num2<1) {
cout << "Wrong Input."
<< endl;
cout << "\nEnter Two
Numbers(Intervals): " << endl;
cin >> num1 >> num2; }
cout << endl << "The
Prime Numbers Between " << num1 << " And " <<
num2 << " Are: " << endl << endl ;
for (n = num1; n <= num2; n++){
count = 0;
for (int i = 2; i <= n/2; i++){
if(n%i==0){
count++;
break;
}}
if(count==0 && n!=1){
cout << n << "\t";
}}
cout << endl;
return 0;
}
C++ Program To Display Prime Numbers
Between Intervals Using Function
#include<iostream>
using namespace std;
int check_prime(int num);
int main(){
int n1,n2,i,flag;
cout << "Enter Two
Numbers(Intervals): " << endl;
cin >> n1 >> n2;
while (n1>=n2 || n1<1 || n2<1)
{
cout << "Wrong Input."
<< endl;
cout << "Enter Two
Numbers(Intervals): " << endl;
cin >> n1 >> n2; }
cout << "\nPrime Numbers
Between " << n1 << " And " << n2 <<
" Are: " << endl << endl;
for(i=n1+1;i<n2;++i) {
flag=check_prime(i);
if(flag==1)
cout << i << "\t";
}
cout << endl;
return 0;
}
int check_prime(int num) {
int j,flag=1;
for(j=2;j<=num/2;++j){
if(num%j==0){
flag=0;
break;
}}
return flag;
}