
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Base 10 Logarithm in C++
Logarithms for base 10 are relatively required for natural computations in a variety of applications. For competitive examinations, there are a few quick ways to memorise a few log values. There are a few ways to compute logarithm results using library functions when programming, as well as a few shortcuts. In this post, we'll go through a couple methods for computing the base-10 logarithm of a given number in C++.
Using log10() function
One library function used to determine the base-10 logarithm for a given parameter is called log10(). An integer or a floating-point number could be the response. It's quite simple to use this method; all you have to do is call the function with a single integer argument and the cmath library to have it calculate the log base 10 for you. Let's look at the syntax and associated programme to see how it is used.
Syntax
#include < cmath > log2( <number> )
Algorithm
- Take a number x as input
- Use log10( x ) to calculate the log base 10 for x
- Return result.
Example
#include <iostream> #include <cmath> using namespace std; float solve( int x ){ float answer; answer = log10( x ); return answer; } int main(){ cout << "Log base 10 for input x = 100 is: " << solve( 100 ) << endl; cout << "Log base 10 for input x = 1000 is: " << solve( 1000 ) << endl; cout << "Log base 10 for input x = 5487 is: " << solve( 5487 ) << endl; cout << "Log base 10 for input x = 25479 is: " << solve( 25479 ) << endl; }
Output
Log base 10 for input x = 100 is: 2 Log base 10 for input x = 1000 is: 3 Log base 10 for input x = 5487 is: 3.73934 Log base 10 for input x = 25479 is: 4.40618
Using logarithm function with some other base
A few intriguing characteristics of the logarithm. We can calculate the output of the logarithm in another base from any base. To compute using any log base say , the following formula is being used.
$$\mathrm{log_{10}\left ( x \right )=\frac{log_{k}\left ( x \right )}{log_{k}\left ( 10 \right )}}$$
Algorithm
- Take a number x as input
- nume := log-base-k ( x )
- deno := log-base-k(10)
- Return ( nume / deno ).
Example
#include <iostream> #include <cmath> using namespace std; float solve( int x ){ float nume, deno; nume = log( x ); deno = log( 10 ); return nume / deno; } int main(){ cout << "Log base 10 for input x = 100 is: " << solve( 100 ) << endl; cout << "Log base 10 for input x = 1000 is: " << solve( 1000 ) << endl; cout << "Log base 10 for input x = 5487 is: " << solve( 5487 ) << endl; cout << "Log base 10 for input x = 25479 is: " << solve( 25479 ) << endl; }
Output
Log base 10 for input x = 100 is: 2 Log base 10 for input x = 1000 is: 3 Log base 10 for input x = 5487 is: 3.73933 Log base 10 for input x = 25479 is: 4.40618
Conclusion
The log10() method of the cmath package can be used to calculate log-base 10. The outcome will be returned as either an integer or a fraction. Another approach is to use a different log base and a simple log formula, as demonstrated in the second part. In order to obtain more accurate results, we can also utilise the numerical method to calculate the logarithm result using bisection, the Newton-Raphson method, or any other non-linear equation-solving technique.