C++ Program to Calculate Logarithm of a Number

C++ Program to Calculate Logarithm of a Number

Logarithms are essential in mathematics and appear everywhere from science and engineering to finance and computing. In programming, knowing how to calculate logarithms helps solve problems involving growth rates, signal processing, and algorithms. C++ makes this simple through the cmath library, which provides functions for natural logarithms, base-10 logarithms, and even custom-base logarithms. This guide introduces beginner-friendly C++ programs that compute logarithms with natural base (e), base 10, and any custom base, making it easy for newcomers to practice and understand.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Calculate Logarithm Using User Input

This program allows users to enter a number and calculates both its natural logarithm (ln) and base-10 logarithm. It’s ideal for beginners to grasp how logarithms work in C++.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double number;

    cout << "Enter a positive number: " << endl;
    cin >> number;

    if (number > 0) {

        double naturalLog = log(number); // ln(x)
        double base10Log = log10(number); // log10(x)

        cout << "Natural logarithm (ln) of " << number << " is: " << naturalLog << endl;
        cout << "Base-10 logarithm of " << number << " is: " << base10Log << endl;

    } else {
        cout << "Logarithm is undefined for zero or negative numbers." << endl;
    }

    return 0;

}

Here, the log() function calculates the natural logarithm, and log10() calculates the base-10 logarithm. Input validation ensures the program does not compute logarithms for zero or negative numbers, helping beginners understand proper error handling.

Program 2: Calculate Logarithm with Predefined Number

Sometimes you want to calculate logarithms without entering input. This program uses a predefined number.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double number = 100.0; // predefined number
    double naturalLog = log(number);
    double base10Log = log10(number);

    cout << "Natural logarithm (ln) of " << number << " is: " << naturalLog << endl;
    cout << "Base-10 logarithm of " << number << " is: " << base10Log << endl;

    return 0;

}

This approach is useful for testing and helps beginners understand the difference between natural logarithms and base-10 logarithms without user input complexity.

Program 3: Calculate Logarithm with Any Custom Base

This program extends the previous one by allowing the user to calculate the logarithm of a number with any positive base other than 1, demonstrating the change-of-base formula.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double number, base;

    cout << "Enter a positive number: " << endl;
    cin >> number;

    if (number <= 0) {

        cout << "Error: Logarithm undefined for zero or negative numbers." << endl;
        return 0;

    }

    cout << "Enter the logarithm base (greater than 0 and not 1): " << endl;
    cin >> base;

    if (base <= 0 || base == 1) {

        cout << "Error: Invalid base. Base must be > 0 and != 1." << endl;
        return 0;

    }

    double customLog = log(number) / log(base); // Change-of-base formula
    cout << "Logarithm of " << number << " with base " << base << " is: " << customLog << endl;

    return 0;

}

This program uses the change of base formula:

$$
\log_b(x) = \frac{\log(x)}{\log(b)}
$$

The program first validates that both the number and the base are positive and that the base is not 1. It then calculates the logarithm using log() from the cmath library. This is an excellent way for beginners to understand how mathematical formulas translate into code.

Frequently Asked Questions (FAQ)

Q: Can I calculate logarithms with bases other than e or 10?
Yes! Use the change of base formula: log_b(x) = log(x)/log(b). Program 3 demonstrates this.

Q: What happens if I enter a negative number or zero?
Logarithms are undefined for zero or negative numbers. Input validation ensures the program handles such cases gracefully.

Q: Why include the cmath library?
cmath provides standard math functions like log(), log10(), and sqrt(), allowing you to perform calculations efficiently.

Conclusion

C++ makes calculating logarithms easy using log(), log10(), and the change of base formula. Beginners can practice using user input, predefined numbers, and custom bases to understand the concepts deeply. By experimenting with these programs, you can strengthen your understanding of both C++ programming and logarithmic mathematics.

Additional & References

For further learning, explore these resources to deepen your understanding:

These resources help beginners experiment with C++ math functions and explore advanced applications.

Scroll to Top