Home » C++ programming language

floor() function with example in C++

C++ floor() function: Here, we are going to learn about the floor() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on April 27, 2019

C++ floor() function

floor() function is a library function of cmath header, it is used to find the roundup (downward) value of the given number, it accepts a number and returns largest integral value that is not greater than to the given number.

Syntax of floor() function:

    floor(x);

Parameter(s): x – is the number whose value to round up.

Return value: double – it returns double type value that is the rounded up value of the given number.

Example:

    Input:
    float x = 2.3;
    Function call:
    floor(x);
    Output:
    2

    Input:
    float x = 3.8
    Function call:
    floor(x);
    Output:
    3

C++ code to demonstrate the example of floor() function

// C++ code to demonstrate the example of 
// floor() function

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    float x;
    
    //input the number
    cout<<"Enter a float value: ";
    cin>>x;
    
    //printing the round up value
    cout<<"floor("<<x<<"): "<<floor(x)<<endl;

    return 0;
}

Output

First run:
Enter a float value: 2.3 
floor(2.3): 2

Second run:
Enter a float value: 3.8 
floor(3.8): 3 

Third run:
Enter a float value: -2.3
floor(-2.3): -3

Fourth run:
Enter a float value: -3.8
floor(-3.8): -4 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.