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

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

#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

Recommended Posts
C++ Program to Find GCD or HCF of Two Numbers Using Recursion
C++ Program to Access Elements of an Array Using Pointer
C++ Program to Check Prime Number
C++ Program to Multiply Two Numbers Using Addition
C++ Program to Store Information of an Employee in Structure
C++ Program to Store Information of a Book in a Structure
C++ Program to Add Two Distances in Inch and Feet
C++ Program to Print Array in Reverse Order
C++ Program to Delete Vowels Characters from String
All C++ Programs