Given a positive integer n, round it to nearest whole number having zero as last digit.
Input : 4722 Output : 4720 Input : 38 Output : 40 Input : 10 Output: 10
Let’s round down the given number n to the nearest integer which ends with 0 and store this value in a variable a.
a = (n / 10) * 10. So, the round up n (call it b) is b = a + 10.
If n – a > b – n then the answer is b otherwise the answer is a.
Below is the implementation of the above approach:
Another method if n is large:
The above method is good only for Integer or Long MAX value. if the input length is greater then the int or long-range above method does not work.
We can solve the problem using String.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Round the given number to nearest multiple of 10 | Set-2
- Nearest prime less than given number n
- Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
- Nearest greater number by interchanging the digits
- Nearest prime number in the array of every array element
- Round-off a number to a given number of significant digits
- Nearest element with at-least one common prime factor
- Replace all consonants with nearest vowels in a string
- Nearest smaller character to a character K from a Sorted Array
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Maximize length of increasing subsequence possible by replacing array element by nearest primes
- Find Nth number in a sequence which is not a multiple of a given number
- Pattern Printing question asked in CGI Coding Round
- Seating arrangement of n boys and girls alternatively around a round table
- Puzzle | Neighbors in a round table
- Seating arrangement of N boys sitting around a round table such that two particular boys sit together
- Check whether a very large number of the given form is a multiple of 3.
- Find First element in AP which is multiple of given prime
- Smallest multiple of 3 which consists of three given non-zero digits
- Smallest multiple of N formed using the given set of digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.