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 



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.