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.
We'll cover the following
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:
For example, if , the Ceiling integer function will convert it to . Similarly, if , the Ceiling integer function will give us .
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; }
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:
For Example, if , the floor integer function will convert it to .
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; }