Related Articles

# Smallest number required to be added to M to make it divisible by N

• Last Updated : 09 Jun, 2021

Given two positive integers M and N, the task is to calculate the smallest number that needs to be added to M, to make it divisible by N.

Examples:

Input: M = 6, N = 7
Output: 1
Explanation: 1 is the smallest number that can be added to 6 to make it divisible by 7.

Input: M = 100, N = 28
Output: 12

Approach: The idea is to find the smallest number greater than or equal to M, that is divisible by N, and then subtract M from it. To get the smallest multiple of N ≥ M, divide M + N by N. If the remainder is 0, then the value is M. Otherwise, the value is M + N – remainder.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the smallest``// number greater than or equal``// to N, that is divisible by k``int` `findNum(``int` `N, ``int` `K)``{``    ``int` `rem = (N + K) % K;` `    ``if` `(rem == 0)``        ``return` `N;``    ``else``        ``return` `N + K - rem;``}` `// Function to find the smallest``// number required to be added to``// to M to make it divisible by N``int` `findSmallest(``int` `M, ``int` `N)``{``    ``// Stores the smallest multiple``    ``// of N, greater than or equal to M``    ``int` `x = findNum(M, N);` `    ``// Return the result``    ``return` `x - M;``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``int` `M = 100, N = 28;` `    ``// Function Call``    ``cout << findSmallest(M, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG{` `// Function to find the smallest``// number greater than or equal``// to N, that is divisible by k``public` `static` `int` `findNum(``int` `N, ``int` `K)``{``    ``int` `rem = (N + K) % K;` `    ``if` `(rem == ``0``)``        ``return` `N;``    ``else``        ``return` `N + K - rem;``}` `// Function to find the smallest``// number required to be added to``// to M to make it divisible by N``public` `static` `int` `findSmallest(``int` `M, ``int` `N)``{``    ` `    ``// Stores the smallest multiple``    ``// of N, greater than or equal to M``    ``int` `x = findNum(M, N);` `    ``// Return the result``    ``return` `x - M;``}` `// Driver Code``public` `static` `void` `main(String args[])``{``    ` `    ``// Given Input``    ``int` `M = ``100``, N = ``28``;` `    ``// Function Call``    ``System.out.println(findSmallest(M, N));``}}` `// This code is contributed by SoumikMondal`

## Python3

 `# Python3 program for the above approach` `# Function to find the smallest``# number greater than or equal``# to N, that is divisible by k``def` `findNum(N, K):``    ` `    ``rem ``=` `(N ``+` `K) ``%` `K` `    ``if` `(rem ``=``=` `0``):``        ``return` `N``    ``else``:``        ``return` `N ``+` `K ``-` `rem` `# Function to find the smallest``# number required to be added to``# to M to make it divisible by N``def` `findSmallest(M, N):``    ` `    ``# Stores the smallest multiple``    ``# of N, greater than or equal to M``    ``x ``=` `findNum(M, N)` `    ``# Return the result``    ``return` `x ``-` `M` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given Input``    ``M ``=` `100``    ``N ``=` `28` `    ``# Function Call``    ``print``(findSmallest(M, N))``    ` `# This code is contributed by ipg2016107`

## C#

 `// C# program for the above approach``using` `System;``        ` `class` `GFG{``    ` `// Function to find the smallest``// number greater than or equal``// to N, that is divisible by k``public` `static` `int` `findNum(``int` `N, ``int` `K)``{``    ``int` `rem = (N + K) % K;`` ` `    ``if` `(rem == 0)``        ``return` `N;``    ``else``        ``return` `N + K - rem;``}`` ` `// Function to find the smallest``// number required to be added to``// to M to make it divisible by N``public` `static` `int` `findSmallest(``int` `M, ``int` `N)``{``    ` `    ``// Stores the smallest multiple``    ``// of N, greater than or equal to M``    ``int` `x = findNum(M, N);`` ` `    ``// Return the result``    ``return` `x - M;``}``    ` `// Driver Code``public` `static` `void` `Main()``{``    ` `    ``// Given Input``    ``int` `M = 100, N = 28;`` ` `    ``// Function Call``    ``Console.WriteLine(findSmallest(M, N));``}``}` `// This code is contributed by susmitakundugoaldanga`

## Javascript

 ``
Output:
`12`

Time Complexity: O(1)
Auxiliary Space: O(1)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up