- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Express an odd number as sum of prime numbers in C++
In this problem, we are given an odd number N. Our task is to express an odd number as the sum of prime numbers.
There can be at max three prime numbers while expressing the number.
Let’s take an example to understand the problem,
Input: N = 55
Output: 53 + 2
Solution Approach:
The odd number can be represented as a sum of primes. Taking these primes into consideration we have three cases.
Case 1: If n is a prime number, it is represented as the sum of one prime number n.
Case 2: If (n - 2) is a prime number, it is represented as the sum of two prime numbers n-2 and 2.
Case 3: ( n - 3 ) is an even number which can be represented as a sum of two prime numbers using Goldbach’s conjecture method in which we will check if a number A is prime and number {(n-3) - A } is also prime then print it.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; bool isPrime(int x) { if (x == 0 || x == 1) return false; for (int i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; } void primeAsSumofPrime(int n) { if (isPrime(n) ) cout<<n; else if (isPrime(n - 2)) cout<<"2 "<<"+ "<<(n - 2); else{ cout<<"3 "<<"+ "; n -= 3; for (int i = 0; i < n; i++) { if (isPrime(i) && isPrime(n - i)) { cout<<i<<" + "<<(n - i); break; } } } } int main() { int n = 561; cout<<"The number "<<n<<" expressed as sum of primes is "; primeAsSumofPrime(n); return 0; }
Output −
The number 561 expressed as sum of primes is 3 + 11 + 547
- Related Articles
- (i) Express 49 as the sum of 7 odd numbers.(ii) Express 121 as the sum of 11 odd numbers.
- Express the sum of 9 as an odd consecutive number.
- Express 144 as sum of 12 odd numbers.
- Express the following numbers as the sum of consecutive odd numbers: $36$.
- Express 18 as the sum of two prime numbers in all possible ways.
- Count ways to express a number as sum of consecutive numbers in C++
- Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++
- Count ways to express ‘n’ as sum of odd integers in C++
- The sum of two prime numbers $(>2)$ is(A) odd (B) even (C) prime (D) even or odd
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime in C++
- How to express 1197 as a product of prime numbers?
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- State whether the following statements are True or False:(a) The sum of three odd numbers is even.(b) The sum of two odd numbers and one even number is even.(c) The product of three odd numbers is odd.(d) If an even number is divided by 2, the quotient is always odd.(e) All prime numbers are odd.(f) Prime numbers do not have any factors.(g) Sum of two prime numbers is always even.(h) 2 is the only even prime number.(i) All even numbers are composite numbers.(j) The product of two even numbers is always even.
- C program for a number to be expressed as a sum of two prime numbers.
- 8.(i) Express 49 as the sum of 7 odd numbers.(ii) Express 121 as the sum of 11 odd numbers.9. How many numbers lie between squares of the following numbers?(i) 12 and 13(ii) 25 and 26(iii) 99 and 100"\n

Advertisements