
- 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 integral solutions of the equation x1 + x2 +…. + xN = k in C++
The solutions for the equation are
- The number of non-negative integral solutions of the equation are $\left(\begin{array}{c}n-k+1\ k\end{array}\right)$
- The number of positive integral solutions of the equation are $\left(\begin{array}{c}k-1\ n-1\end{array}\right)$
Add both to get the required answer. Let's see an example.
Input
n = 4 k = 7
Output
140
Algorithm
- Initialise the numbers n and k.
- Find the integral solutions of not-negative and positive numbers.
- Add both of them.
- Return the answer.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; int factorial(int n) { int product = 1; for (int i = 2; i <= n; i++) { product *= i; } return product; } int nCr(int n, int r) { return factorial(n) / (factorial(n - r) * factorial(r)); } int main() { int n = 4; int k = 7; cout << nCr(n + k - 1, k) + nCr(k - 1, n - 1) &l<t; endl; return 0; }
Output
If you run the above code, then you will get the following result.
140
- Related Articles
- Number of non-negative integral solutions of sum equation in C++
- Compute element-wise arc tangent of x1/x2 choosing the quadrant correctly in Python
- Find the number of solutions to the given equation 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 for the equation x + y + z
- Number of Integral Points between Two Points in C++
- For the following system of equation determine the value of $k$ for which the given system of equation has infinitely many solutions.$(k−3)x+3y=k$ and $kx+ky=12$.
- The formula of the sulphate of an element X is X2(SO4)3. The formula of nitride of element X will be :(a) X2N (b) XN2 (c) XN (d) X2N3
- A quadratic equation with integral coefficient has integral roots. Justify your answer.
- Computing zeroes (solutions) of a mathematical equation in JavaScript
- C/C++ Program for Number of solutions to the Modular Equations?
- Program for Number of solutions to Modular Equations in C/C++?
- Find the Number of Solutions of n = x + n x using C++
- C/C++ Program for Number of solutions to Modular Equations?

Advertisements