
- 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
Find number of solutions of a linear equation of n variables in C++
In this problem, we are given a linear equation of n variable for the form,
coeff1(var1) + coeff2(var2) + … + coeffn(varn) = value
Find the number of solutions of a linear equation of n variables.
Let’s take an example to understand the problem,
Input
coeff[] = {3, 1}, value = 4
Output
1
Explanation
Equation : 3x + y = 4. Solution, x = 0, y = 4.
Solution Approach
A simple solution to the problem is by evaluating the value of the equation. Then update the values by calling it recursively. If the value is 0, then solution count is 1. Else recur with value by subtracting coeff values.
Program to illustrate the working of our solution,
Example
#include<iostream> using namespace std; int countSolutionsEq(int coeff[], int start, int end, int value) { if (value == 0) return 1; int coefCount = 0; for (int i = start; i <= end; i++) if (coeff[i] <= value) coefCount += countSolutionsEq(coeff, i, end, value - coeff[i]); return coefCount; } int main() { int coeff[] = {3, 5, 1, 2}; int value = 6; int n = sizeof(coeff) / sizeof(coeff[0]); cout<<"The number of solutions of the linear equation is "<<countSolutionsEq(coeff, 0, n - 1, value); return 0; }
Output
The number of solutions of the linear equation is 8
- Related Articles
- Program to find number of solutions in Quadratic Equation in C++
- Find the number of solutions to the given equation in C++
- Find the Number of Solutions of n = x + n x using C++
- Number of non-negative integral solutions of sum equation in C++
- Number of integral solutions of the equation x1 + x2 +…. + xN = k in C++
- Find the Number of solutions for the equation x + y + z
- C program to find the solution of linear equation
- What is linear equation in two variables?
- Computing zeroes (solutions) of a mathematical equation in JavaScript
- Solve a linear matrix equation or system of linear scalar equations in Python
- Finding all solutions of a Diophantine equation using JavaScript
- How many solutions does the linear equation $17x + 12y = 30$ have?
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Find M-th number whose repeated sum of digits of a number is N in C++
- Given the linear equation $2x+3y-8=0$, write another linear equation in two variables such that the geometrical representation of the pair so formed is intersecting lines.

Advertisements