text: Number of ways to write N as a sum of K non | C # program for the above approach ; Function to count the number of ways to write N as sum of k non - negative integers ; Initialise [ , ] dp array ; Only 1 way to choose the value with sum K ; Initialise sum ; Count the ways from previous states ; Update the sum ; Return the readonly count of ways ; Driver Code ; Function call
code: using System ; class GFG { static int countWays ( int n , int m ) { int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { dp [ 1 , i ] = 1 ; } int sum ; for ( int i = 2 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { sum = 0 ; for ( int k = 0 ; k <= j ; k ++ ) { sum += dp [ i - 1 , k ] ; } dp [ i , j ] = sum ; } } return dp [ m , n ] ; } public static void Main ( String [ ] args ) { int N = 2 , K = 3 ; Console . Write ( countWays ( N , K ) ) ; } }