C++ Program to Check if Number Can be Expressed as Sum of Two Prime Numbers

Write C++ Program to Check if Number Can be Expressed as Sum of Two Prime Numbers

// CPP Program to Check if Number Can be Expressed as Sum of Two Prime Numbers

#include <iostream>
#include <stdbool.h>

using namespace std;

int check_Prime(int n)
{
	int i;
	bool isprime = true;
	if(n==1 || n==0)
	{
		 isprime = false;
	}

	for(i = 2; i <= n/2; i++)
	{
		if(n%i == 0)
		{
			isprime = false;
			break;
		}
	}
	return isprime;
}

int main()
{
	int n, i, flag = 0;

	cout << "Enter the Number :--> ";
	cin >> n;

	for(i = 2; i <= n; i++)
	{
		if(check_Prime(i))
		{
			if(check_Prime(n - i))
                            cout << i << " + " << n - i << " = " << n << endl;
                        flag = 1;
		}
	}
	if(!flag || n==2 || n==3)
	{
		cout << "Cannot be expressed as sum of 2 positive numbers";
	}

	return 0;
}

Output:

Enter the Number :--> 18
5 + 13 = 18
7 + 11 = 18
11 + 7 = 18
13 + 5 = 18

≪ Previous | Next ≫