Compute the natural logarithm of 2, by adding up to n terms in the series
a. 1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n where n is a positive integer and input by user
Answers
You can have a separate variable, n that you add one to each time, check if it is even with if (n%2 == 0). If that is true, multiply your denominator by -1.
Natural algorithm of logarithm 2 in C++ language.
As per the given question we have to compute the natural logarithm of 2, by adding up to n terms in the series.
a) 1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n where n is a positive integer and input by user.
So, here we have done the programming in C++ language.
# include < iostream >
using namespace std ;
int main ()
{
int i ,n ,sign = -1;
float sum=0;
cout << " Enter the value of n ";
cin >> n;
for( i = 1 ; i < = n ; i++ )
{
sign *= -1;
sum += sign * 1.0 / i;
}
cout << " log 2 : " << sum ;
return 0;
}
The Output Is:
Sample rum 1
Enter the value of n 5
log 2 : 0.783333
Sample run 2
Enter the value of n 10000
log 2 : 0.693091
For more such kind of logarithm questions:
https://brainly.in/question/41400643
https://brainly.in/question/38013487
#SPJ3