c++ - Trying to make a simple recursion exercise -


hi im trying create function : double harmonicsum(int n) calculates , returns sum 1 + ½ + 1/3 + ... + 1/n

this code

double harmonicsum(int n) {   if(n==1) return 1;   return (1.0/n) + (1.0/(harmonicsum(n-1))); } 

it doesnt work . if call harmonicsum(1) or harmonicsum(2) works , harmonicsum(3) no .

correct code

double harmonicsum(int n){ if(n==1) return 1; return (1.0/n) + harmonicsum(n-1); 

}

simply try below double hermonic(int n) {     if(n==1)         return 1 ;     else         return ( float )  1/n + hermonic(n - 1) ; } 

Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -