×

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

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

Syntax of ceil() function:

ceil(x);

Parameter(s)

x – is the number whose value to round up.

Return value

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

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

Example

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