fmod() Function in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The fmod function in C++ is a function of the cmath header file used to find the remainder when two numbers are divided. These two numbers can be of the same or different data types (float, int, double).

Syntax of fmod() Function in C++

The syntax of fmod() c++ function is as follows:

Suppose there is numerator a and denominator b then:

The prototype of the fmod function in C++ is:

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Parameters of fmod() Function in C++

Parameters of the fmod c++ function are:

  • a: it is numerator of type double
  • b: it is the denominator of type double

Return value of fmod() Function in C++

Return Type: double

The fmod C++ function returns a floating-point remainder, a double type value. If the value of the denominator is assigned as zero, then the function returns a value NAN, which means (Not A Number).

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Example

Let us see an example of the fmod C++ function where we will use the fmod() function to return the floating-point remainder.

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

What is the fmod() Function in C++?

In daily life, if we want to do a division or any other mathematical calculations, we do it on paper or use a calculator. But, in a programming language, we do calculations using some built-in functions that perform the calculations.

The fmod c++ function is a library function used to find the remainder value when the numerator and denominator are used as a parameter for the division method.

Suppose we have a and b as numerator and denominator, respectively, which goes under the division method; then we will get a decimal remainder rounded towards zero using the fmod() function in C++.

This fmod c++ function is used to return this remainder value. The formula of the fmod function is as follows:

Where t is referred to as the truncated value for the given parameters- numerator and denominator.

Example: Let numerator be 16, and denominator be 3. 16 goes three times in 3, and 1 remains. So, the return value of using the fmod(16, 3) function will be 1.

fmod() function for arguments of different types.

The fmod c++ function can also be used for different arguments in a program. Suppose we take two variables for doing addition in the c++ program. Here, the two variables (arguments) can be of two data types: integer double or float. Refer to More Examples for more detail.

More Examples

Let's look at more examples to understand the fmod C++ function better.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 1. fmod() function for arguments of different types

Let us see an example where the arguments are of different types.

Output:

Example 2. fmod c++ function for user input.

Output:

Example 3. In this Example, let us see what happens when the value of the denominator is zero.

Output:

In the above example of the fmod c++ function, we can see that if the value of the denominator is zero, then the output we get is NAN. Its stands for Not a Number.

Conclusion

  • fmod function in c++ is used to calculate the remainder of two values: the numerator and the denominator.

  • fmod function in c++ is found in the header file named cmath, so we must include this header file at the beginning of the code before using the function.

  • If the value of the denominator is zero, then the result will be NAN in the fmod C++ function.