×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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

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.

Sample Input and Output

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

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

Example

// 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.