C log() Function

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The log() function in C is a standard library function that computes the natural logarithm (base ( e )) of a given number. It is part of the C standard library (math.h). This function is useful for performing logarithmic calculations.

Table of Contents

  1. Introduction
  2. log() Function Syntax
  3. Understanding log() Function
  4. Examples
    • Computing the Natural Logarithm of a Value
    • Using log() with User Input
  5. Real-World Use Case
  6. Conclusion

Introduction

The log() function calculates the natural logarithm of a given number ( x ). The natural logarithm is the logarithm to the base ( e ) (approximately 2.71828) and is widely used in mathematics, physics, and engineering.

log() Function Syntax

The syntax for the log() function is as follows:

#include <math.h>
double log(double x);

Parameters:

  • x: The value for which the natural logarithm is to be computed. The value must be positive.

Returns:

  • The function returns the natural logarithm of the value x.

Understanding log() Function

The log() function takes a positive value ( x ) as input and returns the natural logarithm of that value. If the input value is negative or zero, the function will return a domain error.

Examples

Computing the Natural Logarithm of a Value

To demonstrate how to use log() to compute the natural logarithm of a value, we will write a simple program.

Example

#include <stdio.h>
#include <math.h>

int main() {
    double value = 2.71828;

    // Compute the natural logarithm of the value
    double natural_log = log(value);

    // Print the result
    printf("Natural logarithm of %.5f is: %.5f\n", value, natural_log);

    return 0;
}

Output:

Natural logarithm of 2.71828 is: 1.00000

Using log() with User Input

This example shows how to use log() to compute the natural logarithm of a value provided by the user.

Example

#include <stdio.h>
#include <math.h>

int main() {
    double value;

    // Get user input for the value
    printf("Enter a positive value: ");
    scanf("%lf", &value);

    // Check if the input value is valid
    if (value <= 0) {
        printf("Invalid input! Please enter a positive value.\n");
        return 1;
    }

    // Compute the natural logarithm of the value
    double natural_log = log(value);

    // Print the result
    printf("Natural logarithm of %.5f is: %.5f\n", value, natural_log);

    return 0;
}

Output (example user input "2.71828"):

Enter a positive value: 2.71828
Natural logarithm of 2.71828 is: 1.00000

Real-World Use Case

Calculating Compound Interest

In real-world applications, the log() function can be used to calculate the time required to reach a certain amount with continuous compounding interest.

Example: Calculating Time for Continuous Compounding

#include <stdio.h>
#include <math.h>

int main() {
    double principal, amount, rate, time;

    // Get user input for the principal amount, desired amount, and interest rate
    printf("Enter the principal amount: ");
    scanf("%lf", &principal);
    printf("Enter the desired amount: ");
    scanf("%lf", &amount);
    printf("Enter the annual interest rate (in decimal): ");
    scanf("%lf", &rate);

    // Check if the input values are valid
    if (principal <= 0 || amount <= 0 || rate <= 0) {
        printf("Invalid input! All values must be positive.\n");
        return 1;
    }

    // Calculate the time required using the natural logarithm
    time = log(amount / principal) / rate;

    // Print the result
    printf("Time required to reach %.2f from %.2f at an annual interest rate of %.2f is: %.2f years\n", amount, principal, rate, time);

    return 0;
}

Output (example user input principal "1000", amount "2000", rate "0.05"):

Enter the principal amount: 1000
Enter the desired amount: 2000
Enter the annual interest rate (in decimal): 0.05
Time required to reach 2000.00 from 1000.00 at an annual interest rate of 0.05 is: 13.86 years

Conclusion

The log() function is essential for computing the natural logarithm of a value in C. It is useful in various mathematical calculations, particularly in fields like mathematics, physics, and engineering, where logarithmic functions are required.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare