Problem Solving: Float and Integer Truncating

Learn to write a program that takes floating point value and print ceiling and floor integers using the concepts we have learned so far.

This lesson will teach us to write a program that takes a floating number and prints floor and ceiling integers.

So let’s get to it!

Ceiling integer

The ceiling integer function gives the smallest nearest integer that is greater than or equal to the specified real value of a floating value. The ceil integer function is denoted by:

ceil(x)=⌈x⌉ceil(x)= \lceil x \rceil

For example, if x=5.5x = 5.5, the Ceiling integer function will convert it to 66. Similarly, if x=−5.5x = -5.5, the Ceiling integer function will give us −5-5.

You may consider the ceil function as moving in the forward (right) direction of the x-axis line and reaching the nearest integer.

Ceiling Integer

x

Ceiling⌈x⌉

Fractional part {x}

5.5

6

0.5

-5.5

-5

0.5

5

5

0

-5

-5

0

Problem statement

Write a program that takes a floating number and prints its ceiling integer.

Sample Input

5.5

Sample output

6

To write a program for the above problem, we first focus on the logic. The logic is that we will take floating values and use the typecasting technique here. Note that when we assign a float value to an integer variable, it stores only the integer part. To make it a ceiling integer, we will add 1 to the positive integer variable. If the number is a negative integer, we will not add any number.

For example, if we assign 5.5 to an integer variable, it holds only 5. So we will add 1 and print 6 as the ceiling integer. If we assign -5.5 to an integer variable, it holds only -5, so we will print -5 as a ceiling integer.

Let us write the complete code below and run it to see the output.

#include <iostream>
using namespace std;

int main()
{
    float num;
    int ceil;
    cout << "num: ";
    cin >> num;
    
    ceil = num; // typecasting floating to integer

    cout << "ceil("<<num<<"): ";
    if (num>0 && ceil!=num) // check if float number is positive 
        cout << ceil+1;
    else
        cout << ceil;
    return 0;
}
Ceiling integer program

In line 11, we are typecasting floating to an integer number.

In lines 14–15, we check if a float num is positive and the integer number is not equal to the floating number, then print ceil+1. For example, if num=5.5, the if statement will be true; hence it will print 6. If num=5, then the condition will be false.

Line 17 will execute when the if statement on line 14 will be false.

Now we have written the complete program for the ceiling integer.

What if we want to check the floor integer? How can we do that?

This is an exercise for you to finish!

Exercise: floor integers

The floor integer function gives the greatest nearest integer that is smaller than or equal to the specified value in a floating value. The floor integer function is denoted by:

floor(x)=⌊x⌋floor(x)= \lfloor x \rfloor

For Example, if x=5.5x = 5.5, the floor integer function will convert it to 55.

Floor Integer

x

Floor ⌊x⌋

Fractional part {x}

5.5

5

0.5

-5.5

-6

0.5

5

5

0

-5

-5

0

Problem statement

Write a program that takes a floating number as input and prints its floor value.

Sample Input

-5.5

Sample output

-6

Instruction: Write your code below in the playground.

#include <iostream>
using namespace std;

int main()
{
    float num;
    int floor;
    cout << "Number: " << endl;
    cin >> num;   

    // Write your code here.
   
    return 0;
}
Floor integer program