

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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 Questions & Answers
- Number of integral solutions of the equation x1 + x2 +…. + xN = k in C++
- Find number of solutions of a linear equation of n variables in C++
- Program to find number of solutions in Quadratic Equation in C++
- Find the number of solutions to the given equation in C++
- Computing zeroes (solutions) of a mathematical equation in JavaScript
- Finding all solutions of a Diophantine equation using JavaScript
- Find the Number of solutions for the equation x + y + z <= n using C++
- Negative number digit sum in JavaScript
- Finding square root of a non-negative number without using Math.sqrt() JavaScript
- Sum a negative number (negative and positive digits) - JavaScript
- Finding the longest non-negative sum sequence using JavaScript
- Number of Integral Points between Two Points in C++
- Non-negative set subtraction in JavaScript
- Secant method to solve non-linear equation
- Find the Number of Solutions of n = x + n x using C++
Advertisements