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

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

php - Prepared Statement Inner Join Cannot Pass Parameter by Reference -

c# - Value cannot be null or empty.\r\nParameter name: name -