I wrote the following sample code to find the harmonic value of N (1 1/2 1/3… 1 / N). Read the comments in the code written with BOLD, and Help me find why this happens.
#include
float harmonic(float n, float har ) {
if(n==0) {
return 0;
}
if(n==1) {
printf("%f ", har+1.0f);***/* This prints value 1.5000*/***
return har+1.0f;
}else{
harmonic(n-1, (har+(1/n)));
}
}
int main()
{
printf(" %f ", harmonic(2, 0.0f)); **/* But this prints value nan(Not a Number)*/**
return 0;
}
< p>Thank you,
Naga
I think you want to do:
p>
return harmonic(n-1, (har+(1/n)));
I wrote the following example Code to find the harmonic value of N (1 1/2 1/3… 1 / N). Read the comments in the code written with BOLD and help me find why this happens.
#include
float harmonic(float n, float har) {
if(n==0) {
return 0;
}
if(n==1) {
printf("%f ", har+1.0f);***/* This prints value 1.5000*/***
return har+1.0f;
}else{
harmonic(n-1, (har+(1/n)));
}
}
int main()
{
printf("%f ", harmonic(2, 0.0f)); **/* But this prints value nan(Not a Number) */**
return 0;
}
Thank you,
Naga
I want You want to do:
return harmonic(n-1, (har+(1/n)));
< /p>