Find the ln ( X ) and log10X with the help of expansion | CPP code to Find the ln x and log < sub > 10 < / sub > x with the help of expansion ; Function to calculate ln x using expansion ; terminating value of the loop can be increased to improve the precision ; Function to calculate log10 x ; Driver Code ; setprecision ( 3 ) is used to display the output up to 3 decimal places
#include <cmath> NEW_LINE #include <iomanip> NEW_LINE #include <iostream> NEW_LINE using namespace std ; double calculateLnx ( double n ) { double num , mul , cal , sum = 0 ; num = ( n - 1 ) / ( n + 1 ) ; for ( int i = 1 ; i <= 1000 ; i ++ ) { mul = ( 2 * i ) - 1 ; cal = pow ( num , mul ) ; cal = cal / mul ; sum = sum + cal ; } sum = 2 * sum ; return sum ; } double calculateLogx ( double lnx ) { return ( lnx / 2.303 ) ; } int main ( ) { double lnx , logx , n = 5 ; lnx = calculateLnx ( n ) ; logx = calculateLogx ( lnx ) ; cout << fixed << setprecision ( 3 ) << " ln ▁ " << n << " ▁ = ▁ " << lnx << endl ; cout << fixed << setprecision ( 3 ) << " log10 ▁ " << n << " ▁ = ▁ " << logx << endl ; }