20. Program that reads a positive number and then computes the logarithm of that value to the base 2.
Chapter 4:
Input And Output
Programming Exercise
Problem # 20:
Write a program that reads a positive number and then computes the logarithm of that value to the base 2.
Solution:
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
float n, lg; //number, logarithm;
cout<<"Enter a value: ";
cin>>n;
// logarith of x with base 2 = natural logarithm with base 10 of x / natural logarithm with base 10 of 2
lg=log10(n)/log10(2); //logarithm with base 2
cout<<"logarithm with base 2: "<<lg<<endl;
getch();
return 0;
}
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
float n, lg; //number, logarithm;
cout<<"Enter a value: ";
cin>>n;
// logarith of x with base 2 = natural logarithm with base 10 of x / natural logarithm with base 10 of 2
lg=log10(n)/log10(2); //logarithm with base 2
cout<<"logarithm with base 2: "<<lg<<endl;
getch();
return 0;
}
Let me know in the comment section if you have any question.
Previous Post:
19. Program that computes the area of a sector of a circle when theta is the angle in radians between the radii.
Comments
Post a Comment