Find number of solutions of a linear equation of n variables | A naive recursive C # program to find number of non - negative solutions for a given linear equation ; Recursive function that returns count of solutions for given RHS value and coefficients coeff [ start . . end ] ; Base case ; Initialize count of solutions ; One by subtract all smaller or equal coefficiants and recur ; Driver Code
using System ; class GFG { static int countSol ( int [ ] coeff , int start , int end , int rhs ) { if ( rhs == 0 ) return 1 ; int result = 0 ; for ( int i = start ; i <= end ; i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ; return result ; } public static void Main ( ) { int [ ] coeff = { 2 , 2 , 5 } ; int rhs = 4 ; int n = coeff . Length ; Console . Write ( countSol ( coeff , 0 , n - 1 , rhs ) ) ; } }