In this C++ tutorial, you will learn how to find base 10 logarithm of given number using log10() function of cmath, with syntax and examples.

C++ log10()

C++ log10() returns Base 10 logarithm of given number (argument).

Syntax

The syntax of C++ log10() is

log10(x)

where

ParameterDescription
xA double, float, long double, or any integral type value. The value of x must be in the range [0, inf].

Returns

The return value depends on the type of value passed for parameter x.

The return value of log10(x) is

  • double if x is double or integral type.
  • float if x is float.
  • long double if x is long double.

The synopsis of log10() function is

double log10 (double x);
float log10 (float x);
long double log10 (long double x);
double log10 (T x); // for integral type argument values

log10() is a function of cmath library. Include cmath library at the start of program, if using log10() function.

ADVERTISEMENT

Example

In this example, we read a value from user into variable x, and find its base-10 logarithm value using log10() function.

C++ Program

#include <iostream>
#include<cmath>
using namespace std;

int main() {
    double x;
    cout << "Enter a number : ";
    cin >> x;
    
    double result = log10(x);
    cout << "log10(" << x << ") : " << result << endl;
}

Output

Enter a number : 100
log10(100) : 2
Program ended with exit code: 0
Enter a number : 1
log10(1) : 0
Program ended with exit code: 0
Enter a number : -8
log10(-8) : nan
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ log10(), and how to use this function to find base 10 logarithm of a number, with the help of examples.