Ceiling function java – Java Program to Get Ceiling Value of a Number

Ceiling function java: In the previous article, we have seen Java Program to Find Cube Root of a Number

In this article we are going to see how to find ceiling value of a given number using Java programming language.

Java Program to Get Ceiling Value of a Number

Before Jumping into the program directly let’s see how to find ceiling value  of any given number.

Explanation:

Ceiling is often used as a rounding function. This is a single-value function.

In java we have Math.ceil() which takes an argument as input and returns a double ceiling value as output.

It returns the closest integer value greater than or equal to a given number and also equal to nearest mathematical integer.

Important-

  • When input argument is an integer then output is also integer.
  • When input argument NaN or an infinity or Positive zero or Negative zero then output is also same as the input argument.
  • When input argument is greater than -1 but less than 0 then output is Negative zero.

Example:

When 
n= 5.3 then ceiling value = 6.0
n= 0.0 then ceiling value = 0.0
n= -463.33 then ceiling value = -463.00
n= -0.11 then ceiling value = -0.0

Let’s see different ways to find ceiling value of a given number.

Method-1: Java Program to Get Ceiling Value of a Number By Using Math.ceil() Method (Static Input)

Approach:

  • Declare double variables to hold input arguments and assign the value to it.
  • Now by using Math.ceil() find the ceiling values of input arguments.
  • Print the result.

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
        //input arguments declared
        double a = -0.11;
        double b = 5.3;
        double c = 0.0;
        double d = -463.33;
        
        //finding ceiling value by using Math.ceil()
        double ceila = Math.ceil(a);
        double ceilb = Math.ceil(b);
        double ceilc = Math.ceil(c);
        double ceild = Math.ceil(d);
        
        //printing the results
        System.out.println("The ceiling value of  " + a + " is " + ceila);
        System.out.println("The ceiling value of  " + b + " is " + ceilb);
        System.out.println("The ceiling value of  " + c + " is " + ceilc);
        System.out.println("The ceiling value of  " + d + " is " + ceild);
    }
}

Output:

The ceiling value of -0.11 is -0.0
The ceiling value of 5.3 is 6.0
The ceiling value of 0.0 is 0.0
The ceiling value of -463.33 is -463.0

Method-2: Java Program to Get Ceiling Value of a Number By Using Math.ceil() Method (Dynamic Input)

Approach:

  • Declare double variables to hold input arguments and take the values of it as user input by using Scanner class.
  • Now by using Math.ceil() find the ceiling values of input arguments.
  • Print the result.

Program:

import java.util.*;
class Main
{
    public static void main(String [] args)
    {
        //Scanner class object created
        Scanner s = new Scanner(System.in);                               
        System.out.println("Enter input argument to find its ceiling value:");
        double a = s.nextDouble();                                           

        //finding ceiling value by using Math.ceil()
        double ceila = Math.ceil(a);
        
        //printing the results
        System.out.println("The ceiling value of  " + a + " is " + ceila);
    }
}

Output:

Enter input argument to find its ceiling value:
5.3
The ceiling value of 5.3 is 6.0

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: