🎓 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 modf() function in C is a standard library function that breaks a floating-point number into its fractional and integral parts. It is part of the C standard library (math.h). This function is useful for separating the fractional and integral components of a floating-point number.
Table of Contents
- Introduction
modf()Function Syntax- Understanding
modf()Function - Examples
- Breaking a Number into Fractional and Integral Parts
- Using
modf()with User Input
- Real-World Use Case
- Conclusion
Introduction
The modf() function decomposes a floating-point number into its fractional and integral parts. The integral part is stored in a separate variable, while the function returns the fractional part.
modf() Function Syntax
The syntax for the modf() function is as follows:
#include <math.h>
double modf(double x, double *intpart);
Parameters:
x: The floating-point number to be decomposed.intpart: A pointer to a variable where the integral part of the number will be stored.
Returns:
- The function returns the fractional part of the number
x.
Understanding modf() Function
The modf() function separates a floating-point number into its fractional and integral parts. The fractional part is returned by the function, while the integral part is stored in the variable pointed to by intpart.
Examples
Breaking a Number into Fractional and Integral Parts
To demonstrate how to use modf() to break a number into its fractional and integral parts, we will write a simple program.
Example
#include <stdio.h>
#include <math.h>
int main() {
double value = 3.14;
double intpart;
// Break the value into fractional and integral parts
double fracpart = modf(value, &intpart);
// Print the result
printf("Value: %.2f\n", value);
printf("Integral part: %.2f\n", intpart);
printf("Fractional part: %.2f\n", fracpart);
return 0;
}
Output:
Value: 3.14
Integral part: 3.00
Fractional part: 0.14
Using modf() with User Input
This example shows how to use modf() to break a user-provided number into its fractional and integral parts.
Example
#include <stdio.h>
#include <math.h>
int main() {
double value, intpart;
// Get user input for the value
printf("Enter a floating-point value: ");
scanf("%lf", &value);
// Break the value into fractional and integral parts
double fracpart = modf(value, &intpart);
// Print the result
printf("Value: %.2f\n", value);
printf("Integral part: %.2f\n", intpart);
printf("Fractional part: %.2f\n", fracpart);
return 0;
}
Output (example user input "5.67"):
Enter a floating-point value: 5.67
Value: 5.67
Integral part: 5.00
Fractional part: 0.67
Real-World Use Case
Handling Monetary Values
In real-world applications, the modf() function can be used to separate the dollar and cent parts of a monetary value.
Example: Handling Monetary Values
#include <stdio.h>
#include <math.h>
int main() {
double amount, dollars;
// Get user input for the monetary amount
printf("Enter the monetary amount: ");
scanf("%lf", &amount);
// Break the amount into dollar and cent parts
double cents = modf(amount, &dollars);
// Print the result
printf("Amount: $%.2f\n", amount);
printf("Dollars: $%.2f\n", dollars);
printf("Cents: %.2f\n", cents * 100); // Convert fractional part to cents
return 0;
}
Output (example user input "123.45"):
Enter the monetary amount: 123.45
Amount: $123.45
Dollars: $123.00
Cents: 45.00
Conclusion
The modf() function is essential for breaking a floating-point number into its fractional and integral parts in C. It is useful in various applications, particularly in fields like finance and engineering, where such decomposition of numbers is 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