C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers

In the previous article, we have discussed C++ Program to Check Prime Number Using User-defined Function. In this article, we will see C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers.

C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers

  • How to split a number into two prime numbers.
  • Write a C++ program to split a number in two prime numbers.

C++ program to Check Whether a Number can be Express as Sum of Two Prime Numbers

C++ program to Check Whether a Number can be Express as Sum of Two Prime Numbers

#include<iostream>
 
using namespace std;
  
int isPrime(int num);
 
int main() {
  int num, i;
  cout << "Enter a positive number\n";
  cin >> num;
  for(i = 2; i <= num/2; i++){
      if(isPrime(i)){
          if(isPrime(num-i)){
         cout << num << " = " << i << " + " << num-i;
              return 0; 
          } 
      }
  }
   
  cout << "Not Possible";
        
  return 0;
}
 
// returns 1 if num is prime number otherwise 0
int isPrime(int num){
    int i;
    // Check whether num is divisible by any 
    // number between 2 to (num/2)
    for(i = 2; i <=(num/2); ++i) {
        if(num%i==0) {
            return 0;
        }
    }
    return 1;
}

Output

Enter a positive number
24
24 = 5 + 19
Enter a positive number
27
Not Possible

Explore various CPP Codes lists that are used in C++ programming to write a sample program. These codes play an important role while learning the programs.