- 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

# 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 Questions & Answers
- 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 for the equation x + y + z <= n using C++
- Find the Number of Solutions of n = x + n x using C++
- Number of non-negative integral solutions of sum equation in C++
- Computing zeroes (solutions) of a mathematical equation in JavaScript
- Finding all solutions of a Diophantine equation using JavaScript
- Number of integral solutions of the equation x1 + x2 +…. + xN = k in C++
- C program to find the solution of linear equation
- Solve a linear matrix equation or system of linear scalar equations in Python
- Which linear function of SciPy is used to solve a banded matrix equation?
- Python program to find number of local variables in a function
- Find the Number of Sextuplets that Satisfy an Equation using C++
- Program to find number of solutions for given equations with four parameters in Python
- Find M-th number whose repeated sum of digits of a number is N in C++

Advertisements