Round the given number to nearest multiple of 10

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to round the given 
// integer to a whole number 
// which ends with zero.
#include <bits/stdc++.h>
using namespace std;
  
// function to round the number
int round(int n)
{
    // Smaller multiple
    int a = (n / 10) * 10;
      
    // Larger multiple
    int b = a + 10;
  
    // Return of closest of two
    return (n - a > b - n)? b : a;
}
  
// driver function
int main()
{
    int n = 4722;
    cout << round(n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA Code for Round the given number
// to nearest multiple of 10
import java.util.*;
  
class GFG {
      
    // function to round the number
    static int round(int n)
    {
        // Smaller multiple
        int a = (n / 10) * 10;
           
        // Larger multiple
        int b = a + 10;
       
        // Return of closest of two
        return (n - a > b - n)? b : a;
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
         int n = 4722;
         System.out.println(round(n));
    }
}
  
// This code is contributed by Arnav Kr. Mandal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to round the given 
# integer to a whole number 
# which ends with zero.
  
# function to round the number
def round( n ):
  
    # Smaller multiple
    a = (n // 10) * 10
      
    # Larger multiple
    b = a + 10
      
    # Return of closest of two
    return (b if n - a > b - n else a)
  
# driver code
n = 4722
print(round(n))
  
# This code is contributed by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Code for Round the given number
// to nearest multiple of 10
using System;
  
class GFG {
      
    // function to round the number
    static int round(int n)
    {
        // Smaller multiple
        int a = (n / 10) * 10;
          
        // Larger multiple
        int b = a + 10;
      
        // Return of closest of two
        return (n - a > b - n)? b : a;
    }
      
    // Driver program 
    public static void Main() 
    {
        int n = 4722;
        Console.WriteLine(round(n));
    }
}
  
// This code is contributed by Vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to round the given integer 
// to a whole number which ends with zero. 
  
// function to round the number 
function roundFunation($n
    // Smaller multiple 
    $a = (int)($n / 10) * 10; 
      
    // Larger multiple 
    $b = ($a + 10); 
  
    // Return of closest of two 
    return ($n - $a > $b - $n) ? $b : $a
  
// Driver Code 
$n = 4722; 
echo roundFunation($n), "\n"
      
// This code is contributed by ajit
?>

chevron_right



Output:

4720


My Personal Notes arrow_drop_up

Intern at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : jit_t