
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- Number of integral solutions of the equation x1 + x2 +…. + xN = k in C++
- Program to find number of solutions in Quadratic Equation in C++
- Find number of solutions of a linear equation of n variables in C++
- Find the number of solutions to the given equation in C++
- Find the Number of solutions for the equation x + y + z
- Negative number digit sum in JavaScript
- Finding the longest non-negative sum sequence using JavaScript
- Sum a negative number (negative and positive digits) - JavaScript
- Finding square root of a non-negative number without using Math.sqrt() JavaScript
- A quadratic equation with integral coefficient has integral roots. Justify your answer.
- Computing zeroes (solutions) of a mathematical equation in JavaScript
- Number of Integral Points between Two Points in C++
- Finding all solutions of a Diophantine equation using JavaScript
- Non-Negative Matrix Factorization
- Non-negative set subtraction in JavaScript

Advertisements