function - How to use nested loop using recursion and check the sum of digits in c++ -


i want calculate number of occurence of particular sum of digits in k digit number. code

long recur(long k, long n) {     long count = 0;     if(k == 0)      {         return 0;     }      for(long = 1; <= 9 ; i++)     {         long c = + recur(k - 1, n);          if(c == n)         {             count++;         }     }     return count; } 

/* k number of digits , n sum of digits want find. example, if k 3, can have numbers 111 999 sum of digits varies 1+1+1=3 9+9+9=27. if want find number of k=3 digit numbers sum(n)=4, answer 3. because 112,121 , 211 possible solutions.*/

it not giving me correct answer.the output 1. appreciated.

try out

long recur(long k, long n) {     if(k==0) return n==0;      long count=0;     for(long i=1; i<=9; ++i) {         count += recur(k - 1, n - i);     }     return count; } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -