1. If num is positive then add 0.5.
2. Else subtract 0.5.
3. Type cast the result to int and return.
num = 1.67, (int) num + 0.5 = (int)2.17 = 2
num = -1.67, (int) num – 0.5 = -(int)2.17 = -2
Time complexity: O(1)
Space complexity: O(1)
Now try rounding for a given precision. i.e., if given precision is 2 then function should return 1.63 for 1.63322 and -1.63 for 1.6332.
- Rounding Floating Point Number To two Decimal Places in C and C++
- How to write a running C code without main()?
- Commonly Asked C Programming Interview Questions | Set 2
- Commonly Asked C Programming Interview Questions | Set 1
- How to count set bits in a floating point number in C?
- Implement your own itoa()
- How to change the output of printf() in main() ?
- How to find length of a string without string.h and loop in C?
- How to measure time taken by a function in C?
- To find sum of two numbers without using any operator
- Print "Even" or "Odd" without using conditional statement
- Implement Your Own sizeof
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Program for Sum the digits of a given number
- How will you print numbers from 1 to 100 without using loop?