 Open in App
Not now

# Program to Subtract two integers of given base

• Difficulty Level : Medium
• Last Updated : 22 Oct, 2021

Given three positive integers X, Y, and B, where X and Y are Base-B integers, the task is to find the value of X – Y such that X >= Y.

Examples:

Input: X = 1212, Y = 256, B = 8
Output: 0734
Explanation: The value of 1212 – 256 in base 8 is 734.

Input: X = 546, Y = 248, B = 9
Output: 287

Approach: The given problem can be solved by using basic mathematics subtraction. Follow the steps below to solve the given problem:

• Initialize two variables say power = 1, carry = 0, to keep track of current power and carry generated while subtracting respectively.
• Initialize a variable, say finalVal = 0, to store the resultant value of X – Y.
• Iterate a loop until X > 0 and perform the following steps:
• Store last digits from the current value of X and Y in two variables, say n1 = X % 10 and n2 = Y % 10 respectively.
• Remove last digits from X and Y by updating X = X / 10 and Y = Y / 10.
• Initialize temp = n1 – n2 + carry.
• If temp < 0, then add base B to N, that is N = N + B and set carry = -1, which will act as a borrow. Otherwise, set carry = 0.
• Add current temp * power to finalVal, that is finalVal = finalVal + temp * power and set power = power * 10.
• After completing the above steps, print the value of finalVal as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to find X - Y in base B` `int` `getDifference(``int` `B, ``int` `X, ``int` `Y)` `{`   `    ``// To store final answer` `    ``int` `finalVal = 0;`   `    ``// To store carry generated` `    ``int` `carry = 0;`   `    ``// To keep track of power` `    ``int` `power = 1;`   `    ``while` `(X > 0) {`   `        ``// Store last digits of current` `        ``// value of X and Y in n1 and` `        ``// n2 respectively` `        ``int` `n1 = X % 10;` `        ``int` `n2 = Y % 10;`   `        ``// Remove last digits from` `        ``// X and Y` `        ``X = X / 10;` `        ``Y = Y / 10;`   `        ``int` `temp = n1 - n2 + carry;`   `        ``if` `(temp < 0) {`   `            ``// Carry = -1 will act` `            ``// as borrow` `            ``carry = -1;` `            ``temp += B;` `        ``}`   `        ``else` `{` `            ``carry = 0;` `        ``}`   `        ``// Add in final result` `        ``finalVal += temp * power;` `        ``power = power * 10;` `    ``}`   `    ``// Return final result` `    ``return` `finalVal;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `X = 1212;` `    ``int` `Y = 256;` `    ``int` `B = 8;`   `    ``cout << (getDifference(B, X, Y));`   `    ``return` `0;` `}`   `    ``// This code is contributed by rakeshsahni`

## Java

 `// Java program for the above approach` `import` `java.io.*;`   `class` `GFG {`   `    ``// Function to find X - Y in base B` `    ``public` `static` `int` `getDifference(` `        ``int` `B, ``int` `X, ``int` `Y)` `    ``{`   `        ``// To store final answer` `        ``int` `finalVal = ``0``;`   `        ``// To store carry generated` `        ``int` `carry = ``0``;`   `        ``// To keep track of power` `        ``int` `power = ``1``;`   `        ``while` `(X > ``0``) {`   `            ``// Store last digits of current` `            ``// value of X and Y in n1 and` `            ``// n2 respectively` `            ``int` `n1 = X % ``10``;` `            ``int` `n2 = Y % ``10``;`   `            ``// Remove last digits from` `            ``// X and Y` `            ``X = X / ``10``;` `            ``Y = Y / ``10``;`   `            ``int` `temp = n1 - n2 + carry;`   `            ``if` `(temp < ``0``) {`   `                ``// Carry = -1 will act` `                ``// as borrow` `                ``carry = -``1``;` `                ``temp += B;` `            ``}`   `            ``else` `{` `                ``carry = ``0``;` `            ``}`   `            ``// Add in final result` `            ``finalVal += temp * power;` `            ``power = power * ``10``;` `        ``}`   `        ``// Return final result` `        ``return` `finalVal;` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `X = ``1212``;` `        ``int` `Y = ``256``;` `        ``int` `B = ``8``;`   `        ``System.out.println(` `            ``getDifference(B, X, Y));` `    ``}` `}`

## Python3

 `# Python3 program for the above approach`   `# Function to find X - Y in base B` `def` `getDifference(B, X, Y) :`   `    ``# To store final answer` `    ``finalVal ``=` `0``;`   `    ``# To store carry generated` `    ``carry ``=` `0``;`   `    ``# To keep track of power` `    ``power ``=` `1``;`   `    ``while` `(X > ``0``) :`   `        ``# Store last digits of current` `        ``# value of X and Y in n1 and` `        ``# n2 respectively` `        ``n1 ``=` `X ``%` `10``;` `        ``n2 ``=` `Y ``%` `10``;`   `        ``# Remove last digits from` `        ``# X and Y` `        ``X ``=` `X ``/``/` `10``;` `        ``Y ``=` `Y ``/``/` `10``;`   `        ``temp ``=` `n1 ``-` `n2 ``+` `carry;`   `        ``if` `(temp < ``0``) :`   `            ``# Carry = -1 will act` `            ``# as borrow` `            ``carry ``=` `-``1``;` `            ``temp ``+``=` `B;`   `        ``else` `:` `            ``carry ``=` `0``;`   `        ``# Add in final result` `        ``finalVal ``+``=` `temp ``*` `power;` `        ``power ``=` `power ``*` `10``;`   `    ``# Return final result` `    ``return` `finalVal;`   `# Driver Code` `if` `__name__ ``=``=` `"__main__"` `:`   `    ``X ``=` `1212``;` `    ``Y ``=` `256``;` `    ``B ``=` `8``;`   `    ``print``(getDifference(B, X, Y));`   `    ``# This code is contributed by AnkThon`

## C#

 `// C# program for the above approach` `using` `System;` `public` `class` `GFG` `{`   `    ``// Function to find X - Y in base B` `    ``public` `static` `int` `getDifference(``int` `B, ``int` `X, ``int` `Y)` `    ``{`   `        ``// To store final answer` `        ``int` `finalVal = 0;`   `        ``// To store carry generated` `        ``int` `carry = 0;`   `        ``// To keep track of power` `        ``int` `power = 1;`   `        ``while` `(X > 0) {`   `            ``// Store last digits of current` `            ``// value of X and Y in n1 and` `            ``// n2 respectively` `            ``int` `n1 = X % 10;` `            ``int` `n2 = Y % 10;`   `            ``// Remove last digits from` `            ``// X and Y` `            ``X = X / 10;` `            ``Y = Y / 10;`   `            ``int` `temp = n1 - n2 + carry;`   `            ``if` `(temp < 0) {`   `                ``// Carry = -1 will act` `                ``// as borrow` `                ``carry = -1;` `                ``temp += B;` `            ``}`   `            ``else` `{` `                ``carry = 0;` `            ``}`   `            ``// Add in final result` `            ``finalVal += temp * power;` `            ``power = power * 10;` `        ``}`   `        ``// Return final result` `        ``return` `finalVal;` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``int` `X = 1212;` `        ``int` `Y = 256;` `        ``int` `B = 8;`   `        ``Console.WriteLine(getDifference(B, X, Y));` `    ``}` `}`   `// This code is contributed by AnkThon`

## Javascript

 ``

Output:

`734`

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

My Personal Notes arrow_drop_up
Related Articles