# Minimum decrements to make integer A divisible by integer B

Given two **positive integers A and B** where A is greater than B. In one move one can decrease A by 1 which implies that after one move A is equal to A – 1. The task is to find the minimum number of moves required to make A divisible by B in constant time.

**Examples:**

Input :A = 10, B = 3Output :1Explanation:Only one move is required A = A - 1 = 9, which is divisible by 3.Input :A = 10, B = 10Output :0Explanation:Since A is equal to B therefore zero move required.

**Approach:**

To solve the problem mentioned above we take the modulus of the numbers that are A % B and the result is stored in a variable which is the required answer.

Below is the implementation of the above approach:

## C++

`// C++ implementation to count ` `// Total numbers moves to make ` `// integer A divisible by integer B ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that print number ` `// of moves required ` `void` `movesRequired(` `int` `a, ` `int` `b) ` `{ ` ` ` `// calculate modulo ` ` ` `int` `total_moves = a % b; ` ` ` ` ` `// print the required answer ` ` ` `cout << total_moves << ` `"\n"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// initialise A and B ` ` ` `int` `A = 10, B = 3; ` ` ` ` ` `movesRequired(A, B); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to count ` `// total numbers moves to make ` `// integer A divisible by integer B ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function that print number ` `// of moves required ` `static` `void` `movesRequired(` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// Calculate modulo ` ` ` `int` `total_moves = a % b; ` ` ` ` ` `// Print the required answer ` ` ` `System.out.println(total_moves); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Initialise A and B ` ` ` `int` `A = ` `10` `, B = ` `3` `; ` ` ` ` ` `movesRequired(A, B); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to count ` `# total numbers moves to make ` `# integer A divisible by integer B ` ` ` `# Function that print number ` `# of moves required ` `def` `movesRequired(a, b): ` ` ` ` ` `# Calculate modulo ` ` ` `total_moves ` `=` `a ` `%` `b ` ` ` ` ` `# Print the required answer ` ` ` `print` `(total_moves) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Initialise A and B ` ` ` `A ` `=` `10` ` ` `B ` `=` `3` ` ` ` ` `movesRequired(A, B) ` ` ` `# This code is contributed by Samarth ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to count ` `// total numbers moves to make ` `// integer A divisible by integer B ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function that print number ` `// of moves required ` `static` `void` `movesRequired(` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// Calculate modulo ` ` ` `int` `total_moves = a % b; ` ` ` ` ` `// Print the required answer ` ` ` `Console.Write(total_moves); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` ` ` `// Initialise A and B ` ` ` `int` `A = 10, B = 3; ` ` ` ` ` `movesRequired(A, B); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

**Output:**

1

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum positive integer divisible by C and is not in range [A, B]
- Count of m digit integers that are divisible by an integer n
- Maximum positive integer divisible by C and is in the range [A, B]
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Minimum numbers needed to express every integer below N as a sum
- Count the minimum steps to reach 0 from the given integer N
- Minimum positive integer to divide a number such that the result is an odd
- Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [2, N]
- Minimum integer that can be obtained by swapping adjacent digits of different parity
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- Minimum number of given moves required to make N divisible by 25
- Minimum operations required to make all Array elements divisible by K
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Minimum number of swaps required to make a number divisible by 60
- Blum Integer
- Find whether a given integer is a power of 3 or not
- Replace all ‘0’ with ‘5’ in an input Integer
- Find One's Complement of an Integer
- Convert given integer X to the form 2^N - 1

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.