
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
The following is an example to check whether a number can be expressed as sum of two prime numbers.
Example
#include <iostream> using namespace std; int func(int num) { int i; int flag = 1; for(i = 2; i <= num/2; ++i) { if(num % i == 0) { flag = 0; break; } } return flag; } int main() { int num , i; cout << "Enter a number : \n"; cin >> num; for(i = 2; i <= num/2; ++i) { if (func(i)) { if (func(num - i)) { cout << num << " = " << i << " + " << num-i << endl; } } } return 0; }
Output
Enter a number : 18 18 = 5 + 13 18 = 7 + 11
In the above program, the function func() is checking that the number is prime or not.
int func(int num) { int i; int flag = 1; for(i = 2; i <= num/2; ++i) { if(num % i == 0) { flag = 0; break; } } return flag; }
In the main() function, a number is entered by the user. It is computing the number as sum of two prime numbers.
cout << "Enter a number : \n"; cin >> num; for(i = 2; i <= num/2; ++i) { if (func(i)) { if (func(num - i)) { cout << num << " = " << i << " + " << num-i << endl; } } }
- Related Articles
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Swift 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
- C program for a number to be expressed as a sum of two prime numbers.
- 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 an integer can be expressed as a sum of two semi-primes in Python
- Check if a number can be expressed as power in C++
- Check if a number can be expressed as a^b in C++
- Check if a number can be expressed as a^b in Python
- Program to check a number can be written as a sum of distinct factorial numbers or not 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 2^x + 2^y in C++
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- List at least three different ways in which 67 be expressed as the sum of three different prime numbers

Advertisements