Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 25: Write a C++ program to check whether the given number can be expressed as the sum of two prime numbers.

#include<iostream>
using namespace std;

int prime(int n)
{
	int check=0;
	for(int i=2;i<=n/2;i++)
	{
		if(n%i==0)
		{
			check=1;
			break;
		}
	}
	if(check==0)
		return 1;
	else
		return 0;
}

int main()
{
	int num,flag=0,temp=0;
	cout<<"\nenter a number : ";
	cin>>num;

	for(int i=2;i<=num/2;i++)
	{
		flag=0;
		flag=prime(i);
		if(flag==1)
		{
			flag=prime(num-i);
			if(flag==1)
			{
				cout<<"\n"<<num<<" = "<<i<<" + "<<num-i;
				temp=1;
			}
		}
	}

	if(temp==0)
	cout<<"\nthis number cannot be expressed as the sum of two prime numbers!!!";
	return 0;
}


If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.