Home » C++ programming language

ceil() function with example in C++

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

C++ ceil() function

ceil() function is a library function of cmath header, it is used to find the roundup (upward) value of the given number, it accepts a number and returns the smallest integral value that is not less than to the given number.

Syntax of ceil() function:

    ceil(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:
    ceil(x);
    Output:
    3

    Input:
    float x = 3.8
    Function call:
    ceil(x);
    Output:
    4

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

// C++ code to demonstrate the example of 
// ceil() 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<<"ceil("<<x<<"): "<<ceil(x)<<endl;

    return 0;
}

Output

First run:
Enter a float value: 2.3 
ceil(2.3): 3

Second run:
Enter a float value: 3.8 
ceil(3.8): 4 

Third run:
Enter a float value: -2.3
ceil(-2.3): -2 

Fourth run:
Enter a float value: -3.8
ceil(-3.8): -3 


Comments and Discussions!

Load comments ↻





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