Geometric Mean for a given K Using Recursion
Example:
For a given K find:
1 + 1/2 + 1/4 + 1/8 + ... + 1/(2^k)
#include <iostream>
#include<math.h>
using namespace std;
///That function should also be of the same dataType as that the answer it is returning
float Gmean(int k,int x,double double sum)
{
if(k==x)
{
sum=sum+(1/(pow(2,k)));
return sum;
}
sum=sum+(1/(pow(2,k)));
double double y=Gmean(k+1,x,sum);
return y;
}
int main()
{
int x;
cin>>x;
double double y=Gmean(0,x,0);
cout<<y;
return 0;
}
For a given K find:
1 + 1/2 + 1/4 + 1/8 + ... + 1/(2^k)
#include <iostream>
#include<math.h>
using namespace std;
///That function should also be of the same dataType as that the answer it is returning
float Gmean(int k,int x,double double sum)
{
if(k==x)
{
sum=sum+(1/(pow(2,k)));
return sum;
}
sum=sum+(1/(pow(2,k)));
double double y=Gmean(k+1,x,sum);
return y;
}
int main()
{
int x;
cin>>x;
double double y=Gmean(0,x,0);
cout<<y;
return 0;
}
Comments
Post a Comment