- Related Questions & Answers
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Check if a number can be expressed as sum two abundant numbers in C++
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Check if a number can be expressed as a^b in C++
- Check if a number can be expressed as power in C++
- Express an odd number as sum of prime numbers in C++
- Check if an integer can be expressed as a sum of two semi-primes in Python
- C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers
- Check if a number can be expressed as a^b in Python
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- Check if a number can be expressed as 2^x + 2^y in C++
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers in C++
- Program to check a number can be written as a sum of distinct factorial numbers or not in Python
- C Program for Find largest prime factor of a number?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Find out if a given number can be expressed as sum of two prime numbers or not.

Given a positive integer N, we need to check if the number N can be represented as a sum of two prime numbers.

Consider an example given below −

20 can be expressed as sum of two prime numbers 3 and 17, 13 and 7.

20= 3+7

20= 13+7

Refer an algorithm given below for expressing a given number as a sum of two prime numbers.

Step 1 − Input the number to be checked at run time.

Step 2 − Repeat from i = 2 to (num/2).

Step 3 − Check i is a prime number.

Step 4 − If i is prime, check if (n - i) is a prime number.

Step 5 − If both (i)and (n - i) are primes, then, the given number can be represented as the sum of primes i and (n - i).

Following is the C program for expressing a given number as a sum of two prime numbers −

#include <stdio.h> int Sum(int n); int main(){ int num, i; printf("Enter number: "); scanf("%d", &num); int flag = 0; for(i = 2; i <= num/2; ++i){ if (sum(i) == 1){ if (sum(num-i) == 1){ printf("\nThe given %d can be expressed as the sum of %d and %d\n\n", num, i, num - i); flag = 1; } } } if (flag == 0) printf("The given %d cannot be expressed as the sum of two prime numbers\n", num); return 0; } //check if a number is prime or not int sum(int n){ int i, isPrime = 1; for(i = 2; i <= n/2; ++i){ if(n % i == 0){ isPrime = 0; break; } } return isPrime; }

When the above program is executed, it produces the following output −

Run 1: Enter number: 34 The given 34 can be expressed as the sum of 3 and 31 The given 34 can be expressed as the sum of 5 and 29 The given 34 can be expressed as the sum of 11 and 23 The given 34 can be expressed as the sum of 17 and 17 Run 2: Enter number: 11 The given 11 cannot be expressed as the sum of two prime numbers

Advertisements