# Multiple of x closest to n

Given two numbers n and x, we need to calculate the smallest value of x that is closest to given number n.

**Examples:**

Input : n = 9, x = 4 Output : 8 Input : n = 2855, x = 13 Output : 2860 Input : n = 46426171, x = 43 Output : 46426154 Input : n = 1, x = 3 Output : 3

We need to find a k such that x*k is closest to n. If we do k = n/x, we get a value of k that may not lead to maximum. We can get closest by comparing the values floor(n/x) * x and ceil(n/x) * x.

Below is an interesting solution that doesn’t require computations of floor(n/x) and ceil(n/x). The idea is to do following two steps.

n = n + x/2; n = n - (n%x); result = n

Let us consider below example

n = 2855 x = 13 n = 2855 + 13/2 = 2861 n = 2861 - (2861 % 13) = 2861 - 1 = 2860

Below are implementations of above steps.

## C++

`// CPP program to calculate the smallest multiple ` `// of x closest to a given number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate the smallest multiple ` `int` `closestMultiple(` `int` `n, ` `int` `x) ` `{ ` ` ` `if` `(x>n) ` ` ` `return` `x; ` ` ` ` ` `n = n + x/2; ` ` ` `n = n - (n%x); ` ` ` `return` `n; ` `} ` ` ` `// driver program ` `int` `main() ` `{ ` ` ` `int` `n = 9, x = 4; ` ` ` `printf` `(` `"%d"` `, closestMultiple(n, x)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to calculate the smallest ` `// multiple of x closest to a given number ` `import` `java.io.*; ` `class` `Solution ` `{ ` ` ` `// Function to calculate the smallest multiple ` ` ` `static` `int` `closestMultiple(` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` `if` `(x>n) ` ` ` `return` `x; ` ` ` `n = n + x/` `2` `; ` ` ` `n = n - (n%x); ` ` ` `return` `n; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `56287` `, x = ` `27` `; ` ` ` `System.out.println(closestMultiple(n, x)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to calculate ` `# the smallest multiple of x ` `# closest to a given number ` ` ` `# Function to calculate ` `# the smallest multiple ` `def` `closestMultiple(n, x): ` ` ` `if` `x > n: ` ` ` `return` `x; ` ` ` `z ` `=` `(` `int` `)(x ` `/` `2` `); ` ` ` `n ` `=` `n ` `+` `z; ` ` ` `n ` `=` `n ` `-` `(n ` `%` `x); ` ` ` `return` `n; ` ` ` `# Driver Code ` `n ` `=` `56287` `; ` `x ` `=` `27` `; ` `print` `(closestMultiple(n, x)); ` ` ` `# This code is contributed ` `# by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to calculate smallest ` `// multiple of x closest to a ` `// given number ` `using` `System; ` ` ` `class` `Solution { ` ` ` `// Function to calculate the ` ` ` `// smallest multiple ` ` ` `static` `int` `closestMultiple(` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` ` ` `if` `(x > n) ` ` ` `return` `x; ` ` ` `n = n + x / 2; ` ` ` `n = n - (n % x); ` ` ` `return` `n; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 56287, x = 27; ` ` ` `Console.WriteLine(closestMultiple(n, x)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to calculate ` `// the smallest multiple ` `// of x closest to a ` `// given number ` ` ` `// Function to calculate ` `// the smallest multiple ` `function` `closestMultiple(` `$n` `, ` `$x` `) ` `{ ` ` ` `if` `(` `$x` `> ` `$n` `) ` ` ` `return` `$x` `; ` ` ` ` ` `$n` `= ` `$n` `+ ` `$x` `/ 2; ` ` ` `$n` `= ` `$n` `- (` `$n` `% ` `$x` `); ` ` ` `return` `$n` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 9; ` ` ` `$x` `= 4; ` ` ` `echo` `closestMultiple(` `$n` `, ` `$x` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

56295

This article is contributed by **Pramod Kumar**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find the multiple of x which is closest to a^b
- Find multiple of x closest to or a ^ b (a raised to power b)
- Bitwise AND of sub-array closest to K
- Find K Closest Points to the Origin
- Find the number closest to n and divisible by m
- Closest perfect square and its distance
- Closest sum partition (into two subsets) of numbers from 1 to n
- Find the closest and smaller tidy number
- Find a triplet in an array whose sum is closest to a given number
- Find number from given list for which value of the function is closest to A
- Minimum absolute difference of a number and its closest prime
- Multiple Inheritance in C++
- Find the largest possible k-multiple set
- Java and Multiple Inheritance
- Nth number whose sum of digit is multiple of 10