











C++ Program to check whether a number be expressed as a sum of two prime numbers
Program to check whether a number be expressed as a sum of two prime numbers
Prime number is a number which only have two divisors i.e. a number which can not be divided by any other number other than 1 or itself is a prime number.
There are many theories which express numbers as a sum of two primes like Goldbach’s Conjecture which states that any even number greater than 2 can be expressed as a sum of two primes.
Here we will check for all the numbers if they can be expressed as sum of two primes or not.




Algorithm:-
- Take number as input from user.
- Input is stored in an int type variable say num
- A loop is started from i = 1 and will work till i <= num/2
- Every value of i is checked for being a prime number.
- If i is a prime number then num – i is checked for being a prime.
- If both are prime then print the result
- Otherwise num cannot be expressed as a sum of two prime numbers.
C++ Code:-
//C++ Program
//Number expressed as sum of two prime numbers
#include<iostream>
using namespace std;
// Function to check prime number
int Prime(int num)
{
int div=0;
for(int i=1;i<=num;i++)
{
if(num%i==0)
div++;
}
if(div==2)
return 1;
return 0;
}
int main()
{
int check = 0, n;
cout<< “Enter a positive integer: “;
//user input
cin>>n;
for(int i = 1; i <= n/2;i++)
{
// condition for i to be a prime number
if (Prime(i))
{
// condition for n-i to be a prime number
if (Prime(n–i))
{
cout<<n <<” = “<< i <<” + “ << n–i<< endl;
check = 1;
}
}
}
if (check == 0)
cout<<n<<” cannot be expressed as the sum of two prime numbers.”;
return 0;
}
Output
Enter a positive integer: 15
15 = 2 + 13


- Highest Common Factor(HCF): C | C++ | Java | Python
- Lowest Common Multiple (LCM) : C | C++ | Java | Python
- Greatest Common Divisor : C | C++ | Java | Python
- Binary to Decimal to conversion : C | C++ | Java | Python
- Binary to Octal conversion : C | C++ | Java | Python
- Decimal to Binary conversion: C | C++ | Java | Python
- Decimal to octal Conversion: C | C++ | Java | Python
- Octal to Binary conversion : C | C++ | Java | Python
- Octal to Decimal conversion : C | C++ | Java | Python
- Quadrants in which a given coordinate lies : C | C++ | Java | Python
- Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
- Maximum number of handshakes: C | C++ | Java | Python
- Addition of two fractions: C | C++ | Java | Python
- Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
- Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
- Count possible decoding of a given digit sequence : C | C++ | Java
- Check whether a character is a vowel or consonant : C | C++ | Java | Python
- Check whether a character is a alphabet or not : C | C++ | Java | Python
- Calculate the area of a circle : C | C++ | Java | Python
- Find the ASCII value of a character : C | C++ | Java | Python
- Find the prime numbers between 1 to 100 : C | C++ | Java | Python
- Calculate the number of digits in an integer : C | C++ | Java | Python
- Convert digit/number to words : C | C++ | Java | Python
- Counting number of days in a given month of a year: C | C++ | Java | Python
- Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
- Finding number of integers which has exactly x divisors: C | C++ | Java | Python
- Finding Roots of a quadratic equation : C | C++ | Java | Python
Login/Signup to comment