C++ program to find natural log of 2 by solving a term step by step guide (logic explained) 

C++ program to find natural log of 2 by solving a term

This is a C++ program to find natural log of 2 by solving following term :
1/1 - 1/2 + 1/3 - 1/4 + 1/5. .. 
Steps
User side:
1: User asked to enter the numbers of terms.
2: User displayed with the natural log.
Developer side
1: Declare variables that are going to be used in program.
2: Input the numbers of terms from user and store it in a variable 'n'.
3: Initialize the variable 'm' with -1.
4: Start a while loop from one to numbers of terms entered by user.
5: In the loop assign 'm' with m*-1.
6: Store log in the variable 'log' using following statement  log=log+(m/i); and increase the counter by 1. While loop ends here.
7: Display the log outside the loop .
8: Program ends here.

Code:
#include<iostream>
using namespace std;

int main()
{      //Variable declaration
       int i=1;
       float n,m,log=0;
       //heading
       cout<<"\t\"This program is to find natural log of 2\""<<endl;
       //input of number of terms
       cout<<"Enter the number of terms"<<endl;
       cin>>n;
       m=-1;
       //while loop to solve the series
       while(i<=n)
       { m=m*-1;
       log=log+(m/i);
       i++;
       }
       //output of result
       cout<<"The natural log of 2 is equal to  "<<endl<<log<<endl;

       system("pause");
       return 0;
}


Output:

Comments