Advertisement
a3f

Calculate natural logarithm of 2

a3f
Dec 3rd, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main (void)
  4. {
  5.     /*
  6.     Write a program that prompts the user to enter a number then compute the natural logarithm of 2,
  7.     by adding up to n terms in the series where n is the number that the user entered.
  8.  
  9.     1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
  10.     */
  11.  
  12.     double sum = 0;
  13.     int n = 1000;
  14.     for (int i=1; i <= n; i=i+2)
  15.     {
  16.         sum += 1.0/i - 1.0/(i+1);
  17.     }
  18.     std::cout << sum;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement