
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Number Can Be Expressed as Sum of Two Prime Numbers in C++
The following is an example to check whether a number can be expressed as sum of two prime numbers.
Example
#include <iostream> using namespace std; int func(int num) { int i; int flag = 1; for(i = 2; i <= num/2; ++i) { if(num % i == 0) { flag = 0; break; } } return flag; } int main() { int num , i; cout << "Enter a number : \n"; cin >> num; for(i = 2; i <= num/2; ++i) { if (func(i)) { if (func(num - i)) { cout << num << " = " << i << " + " << num-i << endl; } } } return 0; }
Output
Enter a number : 18 18 = 5 + 13 18 = 7 + 11
In the above program, the function func() is checking that the number is prime or not.
int func(int num) { int i; int flag = 1; for(i = 2; i <= num/2; ++i) { if(num % i == 0) { flag = 0; break; } } return flag; }
In the main() function, a number is entered by the user. It is computing the number as sum of two prime numbers.
cout << "Enter a number : \n"; cin >> num; for(i = 2; i <= num/2; ++i) { if (func(i)) { if (func(num - i)) { cout << num << " = " << i << " + " << num-i << endl; } } }
Advertisements