Round the given number to nearest multiple of 10
Last Updated :
20 Feb, 2023
Given a positive integer n, round it to nearest whole number having zero as last digit.
Examples:
Input : 4722
Output : 4720
Input : 38
Output : 40
Input : 10
Output: 10
Approach:
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:
C++
#include <bits/stdc++.h>
using namespace std;
int round(int n)
{
int a = (n / 10) * 10;
int b = a + 10;
return (n - a > b - n)? b : a;
}
int main()
{
int n = 4722;
cout << round(n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int round(int n)
{
int a = (n / 10) * 10;
int b = a + 10;
return (n - a > b - n)? b : a;
}
public static void main(String[] args)
{
int n = 4722;
System.out.println(round(n));
}
}
|
Python3
def round( n ):
a = (n // 10) * 10
b = a + 10
return (b if n - a > b - n else a)
n = 4722
print(round(n))
|
C#
using System;
class GFG {
static int round(int n)
{
int a = (n / 10) * 10;
int b = a + 10;
return (n - a > b - n)? b : a;
}
public static void Main()
{
int n = 4722;
Console.WriteLine(round(n));
}
}
|
PHP
<?php
function roundFunation($n)
{
$a = (int)($n / 10) * 10;
$b = ($a + 10);
return ($n - $a > $b - $n) ? $b : $a;
}
$n = 4722;
echo roundFunation($n), "\n";
?>
|
Javascript
<script>
function round(n)
{
let a = parseInt(n / 10, 10) * 10;
let b = a + 10;
return (n - a > b - n)? b : a;
}
let n = 4722;
document.write(round(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Another method if n is large:
The above method is good only for Integer or Long MAX value. if the input length is greater than the integer or long-range above method does not work.
We can solve the problem using String.
C++
#include <bits/stdc++.h>
using namespace std;
string Round(string s, int n)
{
string c = s;
if(c[n - 1] == '0')
return s;
else if(c[n - 1] == '1' || c[n - 1] == '2' ||
c[n - 1] == '3' || c[n - 1] == '4' ||
c[n - 1] == '5' )
{
c[n - 1] = '0';
return c;
}
else
{
c[n - 1] = '0';
for(int i = n - 2 ; i >= 0 ; i--)
{
if(c[i] == '9')
c[i] = '0';
else
{
int t = c[i] - '0' + 1;
c[i] = (char)(48 + t);
break;
}
}
}
string s1 = c;
if(s1[0] == '0')
s1 = "1" + s1;
return s1;
}
int main()
{
string s="5748965412485599999874589965999";
int n=s.length();
cout << Round(s,n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static String round(String s, int n)
{
char[] c=s.toCharArray();
if(c[n-1]=='0')
return s;
else if(c[n-1] == '1' || c[n-1] == '2' ||
c[n-1] == '3' || c[n-1] == '4' ||
c[n-1] == '5' )
{
c[n-1]='0';
return new String(c);
}
else
{
c[n-1]='0';
for(int i = n - 2 ; i >= 0 ; i--)
{
if(c[i] == '9')
c[i]='0';
else
{
int t= c[i] - '0' + 1;
c[i]=(char)(48+t);
break;
}
}
}
String s1=new String(c);
if(s1.charAt(0) == '0')
s1="1"+s1;
return s1;
}
public static void main (String[] args)
{
String s="5748965412485599999874589965999";
int n=s.length();
System.out.println(round(s,n));
}
}
|
Python3
def Round(s, n):
s = list(s)
c = s.copy()
if (c[n - 1] == '0'):
return ("".join(s))
elif (c[n - 1] == '1' or c[n - 1] == '2' or
c[n - 1] == '3' or c[n - 1] == '4' or
c[n - 1] == '5'):
c[n - 1] = '0'
return ("".join(c))
else:
c[n - 1] = '0'
for i in range(n - 2, -1, -1):
if (c[i] == '9'):
c[i] = '0'
else:
t = ord(c[i]) - ord('0') + 1
c[i] = chr(48 + t)
break
s1 = "".join(c)
if (s1[0] == '0'):
s1 = "1" + s1
return s1
s = "5748965412485599999874589965999"
n = len(s)
print(Round(s, n))
|
C#
using System;
class GFG {
static string round(string s, int n)
{
char[] c = s.ToCharArray();
if(c[n - 1] == '0')
return s;
else if(c[n - 1] == '1' || c[n - 1] == '2' ||
c[n - 1] == '3' || c[n - 1] == '4' ||
c[n - 1] == '5' )
{
c[n - 1] = '0';
return new string(c);
}
else
{
c[n - 1] = '0';
for(int i = n - 2 ; i >= 0 ; i--)
{
if(c[i] == '9')
c[i] = '0';
else
{
int t = c[i] - '0' + 1;
c[i] = (char)(48 + t);
break;
}
}
}
string s1 = new string(c);
if(s1[0] == '0')
s1 = "1" + s1;
return s1;
}
static void Main() {
string s="5748965412485599999874589965999";
int n=s.Length;
Console.WriteLine(round(s,n));
}
}
|
Javascript
<script>
function round(s, n)
{
let c = s.split('');
// Last character is 0 then return the
// original string
if (c[n - 1] == '0')
return s;
// If last character is
// 1 or 2 or 3 or 4 or 5 make it 0
else if (c[n - 1] == '1' || c[n - 1] == '2' ||
c[n - 1] == '3' || c[n - 1] == '4' ||
c[n - 1] == '5' )
{
c[n - 1] = '0';
return c.join("");
}
else
{
c[n - 1] = '0';
// process carry
for(let i = n - 2 ; i >= 0 ; i--)
{
if (c[i] == '9')
c[i] = '0';
else
{
let t = c[i].charCodeAt() -
'0'.charCodeAt() + 1;
c[i] = String.fromCharCode(48 + t);
break;
}
}
}
let s1 = c.join("");
if (s1[0] == '0')
s1 = "1" + s1;
return s1;
}
let s = "5748965412485599999874589965999";
let n = s.length;
document.write(round(s,n));
</script>
|
Output
5748965412485599999874589966000
Time Complexity: O(N) where N is length of string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...