For the lab assignment, you are to write a function to calculate the logarithm of a number for an arbitrary base. This function should be named logAny() and it should take parameters of the number whose log you wish to calculate, and the base to be used. You may write any functions you wish to implement this program, in addition to the following functions. However, you must
implement the following functions: double logAny(double x, double b) – This function returns the log base b of x. For example, logAny(8,2) should return 3 (because 2 to the 3rd power is 8 and thus 3 is the log base 2 of 8). int main(void) – Of course, you need to write a main().
Code in C
Code :
I have used inbuilt function in C to calculate log().
This log() function is in math.h header file, so I included that too, in my program.
#include <stdio.h>
#include <math.h>
//function to calculate logx where base is b
double logAny(double x, double b)
{
float p = log(x);
float q = log(b);
return p/q;
}
int main()
{
printf("Enter the number and its base : ");
float x,b;
scanf("%f%f", &x,&b);
float ans = logAny(x,b);
printf("%0.3f", ans);
return 0;
}
Output :
Try running above code on your own system.
Like, if this helped:)
Get Answers For Free
Most questions answered within 1 hours.