- 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

# C++ code to find three numbers whose sum is n

Suppose we have a number n. We are going to find three numbers a, b and c, such that a + b + c = n and none of these three numbers are multiple of 3.

So, if the input is like n = 233, then the output will be [77, 77, 79]

## Steps

To solve this, we will follow these steps −

if (n - 2) mod 3 is same as 0, then: return 1, 2, and n - 3 Otherwise return 1, 1, and n - 2

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; void solve(int n){ if ((n - 2) % 3 == 0) cout << 1 << ", " << 2 << ", " << n - 3; else cout << 1 << ", " << 1 << ", " << n - 2; } int main(){ int n = 233; solve(n); }

## Input

233

## Output

1, 2, 230

- Related Articles
- C++ code to find composite numbers whose difference is n
- C++ program to find range whose sum is same as n
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Find N distinct numbers whose bitwise Or is equal to K in C++
- C++ code to find greater number whose factor is k
- C++ code to find palindrome string whose substring is S
- Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript
- Maximum Primes whose sum is equal to given N in C++
- Find a Number X whose sum with its digits is equal to N in C++
- Find two consecutive whole numbers whose sum is 19.
- Count numbers whose difference with N is equal to XOR with N in C++
- Count numbers whose XOR with N is equal to OR with N in C++
- Program to find sum of first n natural numbers in C++
- Find the Minimum Number of Fibonacci Numbers Whose Sum Is K in C++

Advertisements