🎓 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 llrint() function in C is a standard library function that rounds a given floating-point number to the nearest integer value and then casts the result to a long long int. It is part of the C standard library (math.h). This function is useful for performing mathematical rounding operations where the result is needed as a long long int.
Table of Contents
- Introduction
llrint()Function Syntax- Understanding
llrint()Function - Examples
- Rounding and Casting a Value to Long Long Integer
- Using
llrint()with User Input
- Real-World Use Case
- Conclusion
Introduction
The llrint() function calculates the nearest integer to a given floating-point number ( x ) and then casts the result to a long long int. This function follows the current rounding mode specified by the floating-point environment, which can be controlled using the fesetround() function from <fenv.h>.
llrint() Function Syntax
The syntax for the llrint() function is as follows:
#include <math.h>
long long int llrint(double x);
Parameters:
x: The floating-point value to be rounded.
Returns:
- The function returns the nearest integer to
xas along long int.
Understanding llrint() Function
The llrint() function takes a floating-point number ( x ) and returns the nearest integer value as a long long int. This function rounds according to the current rounding mode set in the floating-point environment.
Examples
Rounding and Casting a Value to Long Long Integer
To demonstrate how to use llrint() to round a value to the nearest long long integer, we will write a simple program.
Example
#include <stdio.h>
#include <math.h>
int main() {
double value = 3.14;
// Compute the rounded value
long long int result = llrint(value);
// Print the result
printf("Rounded value of %.2f to long long int is: %lld\n", value, result);
return 0;
}
Output:
Rounded value of 3.14 to long long int is: 3
Using llrint() with User Input
This example shows how to use llrint() to round a value provided by the user to the nearest long long integer.
Example
#include <stdio.h>
#include <math.h>
int main() {
double value;
// Get user input for the value
printf("Enter a value: ");
scanf("%lf", &value);
// Compute the rounded value
long long int result = llrint(value);
// Print the result
printf("Rounded value of %.2f to long long int is: %lld\n", value, result);
return 0;
}
Output (example user input "2.7"):
Enter a value: 2.7
Rounded value of 2.70 to long long int is: 3
Real-World Use Case
Calculating Large Quantities in Inventory Management
In real-world applications, the llrint() function can be used to calculate the number of large quantities in inventory management, where the result must be a whole number represented as a long long int.
Example: Calculating Large Quantities
#include <stdio.h>
#include <math.h>
int main() {
double items_per_box;
double total_items;
long long int boxes_needed;
// Get user input for the number of items per box and total items
printf("Enter the number of items per box: ");
scanf("%lf", &items_per_box);
printf("Enter the total number of items: ");
scanf("%lf", &total_items);
// Calculate the number of boxes needed
boxes_needed = llrint(total_items / items_per_box);
// Print the result
printf("Number of boxes needed: %lld\n", boxes_needed);
return 0;
}
Output (example user input items_per_box "10.0" and total_items "95.0"):
Enter the number of items per box: 10.0
Enter the total number of items: 95.0
Number of boxes needed: 10
Conclusion
The llrint() function is essential for rounding a floating-point number to the nearest integer and casting it to a long long int in C. It is useful in various mathematical calculations, particularly in fields like finance, engineering, and inventory management, where the result must be an integer represented as a long long int.
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