🎓 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
- Introduction
log()Function Syntax- Understanding
log()Function - Examples
- Computing the Natural Logarithm of a Value
- Using
log()with User Input
- Real-World Use Case
- 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:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment