# Number of non-negative integral solutions of sum equation in C++

In this tutorial, we are going to write a program that finds the number non-negative integral solution of sum equation.

The sum equation is x + y + z = n. You are given the number n, you need to find the number of solutions for the equation. Let's see an example.

Input

2

Output

6

Solutions are

0 0 2
0 1 1
0 2 0
1 0 1
1 1 0
2 0 0

## Algorithm

• Initialise the number m.

• Initialise the count to 0.

• Write three nested loops to get all the combinations of three numbers.

• Check the validation of the equation.

• If the current numbers satisfies the equation, then increment the count.

• Return the count.

## Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h>
using namespace std;
int getEquationSolutionCount(int n) {
int count = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n - i; j++) {
for (int k = 0; k <= n - i - j; k++) {
if (i + j + k == n) {
count++;
}
}
}
}
return count;
}
int main() {
int n = 10;
cout << getEquationSolutionCount(n) << endl;
return 0;
}

## Output

If you run the above code, then you will get the following result.

66