Computer Science, asked by dungavathhima2002, 2 months ago

write a c++program,check wether a number can be expressed as sum of two prime numbers.​

Answers

Answered by vojid254721
0

Answer:

Explanation:

the number can be expressed as sum of two prime numbers, the output shows the combination of the prime numbers.

To perform this task, a user-defined function is created to check prime number.

Example: Check Whether a Number can be Expressed as a Sum of Two Prime Numbers

#include <iostream>

using namespace std;

bool checkPrime(int n);

int main() {

int n, i;

bool flag = false;

cout << "Enter a positive integer: ";

cin >> n;

for(i = 2; i <= n/2; ++i) {

if (checkPrime(i)) {

if (checkPrime(n - i)) {

cout << n << " = " << i << " + " << n-i << endl;

flag = true;

}

}

}

if (!flag)

cout << n << " can't be expressed as sum of two prime numbers.";

return 0;

}

// Check prime number

bool checkPrime(int n)

{

int i;

bool isPrime = true;

// 0 and 1 are not prime numbers

if (n == 0 || n == 1) {

isPrime = false;

}

else {

for(i = 2; i <= n/2; ++i) {

if(n % i == 0) {

isPrime = false;

break;

}

}

}

return isPrime;

}

Answered by shivam7114
0

Answer:

gshshjekekekmemen nn

Explanation:

mark it as brainliest

Similar questions