C++ ceil()

Last Updated On Monday 18th Apr 2022

ceil function

ceil c

The C++ ceil() function returns the smallest possible integer value which is greater than or equal to the given argument. It is defined in the <cmath> header file.

Syntax

	ceil(double num);
	

Parameters

The ceil() function takes a one argument which num is floating-point number whose ceiling value is to be round up.

Return value

Returns the smallest integral value that is not less than num.

double ceil

ceil() prototype

	double ceil(double num);
float ceil(float num);
long double ceil(long double num);
// for integral types
double ceil(T num);
	

std ceil

Example 1 : C++ ceil()

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

int main() {
  double num = 11.11;
  double result = ceil(num);
  cout << "Ceil of " << num << " = " << result;
  return 0;
}
	

Output

	Ceil of 11.11 = 12
	

math ceiling

Example 2 : C++ ceil()

	#include <stdio.h>
#include <math.h>
int main () {
  printf ("Ceil of 1.3 is %.1f\n", ceil(1.3) );
  printf ("Ceil of 5.8 is %.1f\n", ceil(5.8) );
  printf ("Ceil of -1.3 is %.1f\n", ceil(-1.3) );
  printf ("Ceil of -5.8 is %.1f\n", ceil(-5.8) );
  return 0;
}
	

Output

	Ceil of 2.3 is 2.0
Ceil of 3.8 is 6.0
Ceil of -2.3 is -1.0
Ceil of -3.8 is -5.0