Open In App

Program to Division two integers of given base

Last Updated : 25 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given three positive integers Base B, Dividend, and Divisor, where Dividend and Divisor are Base-B integers, the task is to find dividend/divisor.

Examples:

Input: Dividend = 513, Divisor = 7, B = 8
Output: 57
Explanation: The value of 513/7 in base 8 is 57.

Input: Dividend = 400, Divisor = 20 B = 8
Output: 20

Approach: To solve the problem follow the below idea: 

The given problem can be solved by using basic mathematics division.

Steps that were to follow:

  • Convert the dividend and divisor from base B to base 10 using the toDecimal function.
  • Initialize decimal = 0 and power = 0
  • Repeat the following steps while dividend != 0:
    • Extract the last digit of the dividend and store it in the digit
    • Add digit * B^power to decimal
    • Divide the dividend by 10
    • Increment power by 1
  • Convert the divisor from base B to base 10 using the same algorithm as above
  • Perform the division in base 10 to get the quotient using the operator /: quotient = dividend/divisor
  • Convert the quotient from base 10 to base B using the fromDecimal function:
  • Initialize num = 0 and power = 0
  • Repeat the following steps while quotient != 0:
    • Calculate digit = quotient % B
    • Add digit * 10^power to num
    • Divide quotient by B
    • Increment power by 1
    • Reverse the digits of the num
  • Print the quotient in base B

Below is the code to implement the above approach:

C++




// C++ program for Division two integers
// of given base
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert a number from
// base B to base 10
int toDecimal(int num, int B)
{
    int decimal = 0, power = 0;
    while (num != 0) {
        int digit = num % 10;
        decimal += digit * pow(B, power);
        num /= 10;
        power++;
    }
    return decimal;
}
 
// Function to convert a number
// from base 10 to base B
int fromDecimal(int decimal, int B)
{
    int num = 0, power = 0;
    while (decimal != 0) {
        int digit = decimal % B;
        num += digit * pow(10, power);
        decimal /= B;
        power++;
    }
    return num;
}
 
// Function to divide two numbers
// in base B
int divide(int B, int dividend, int divisor)
{
    // Convert the numbers to base 10
    int dividend_decimal = toDecimal(dividend, B);
    int divisor_decimal = toDecimal(divisor, B);
 
    // Perform the division in base 10
    int quotient_decimal
        = dividend_decimal / divisor_decimal;
 
    // Convert the quotient to base B
    int quotient = fromDecimal(quotient_decimal, B);
 
    return quotient;
}
 
// Driver code
int main()
{
    int B = 8; // Base
 
    // Dividend in base B
    int dividend = 513;
 
    // Divisor in base B
    int divisor = 7;
 
    int quotient = divide(B, dividend, divisor);
 
    cout << quotient << endl;
 
    return 0;
}


Java




/*Java program for Division two integers of given base */
 
import java.lang.Math;
 
public class Main {
 
    // Function to convert a number from base B to base 10
    static int toDecimal(int num, int B)
    {
        int decimal = 0, power = 0;
        while (num != 0) {
            int digit = num % 10;
            decimal += digit * Math.pow(B, power);
            num /= 10;
            power++;
        }
        return decimal;
    }
 
    // Function to convert a number from base 10 to base B
    static int fromDecimal(int decimal, int B)
    {
        int num = 0, power = 0;
        while (decimal != 0) {
            int digit = decimal % B;
            num += digit * Math.pow(10, power);
            decimal /= B;
            power++;
        }
        return num;
    }
 
    // Function to divide two numbers in base B
    static int divide(int B, int dividend, int divisor)
    {
        // Convert the numbers to base 10
        int dividend_decimal = toDecimal(dividend, B);
        int divisor_decimal = toDecimal(divisor, B);
 
        // Perform the division in base 10
        int quotient_decimal = dividend_decimal / divisor_decimal;
 
        // Convert the quotient to base B
        int quotient = fromDecimal(quotient_decimal, B);
 
        return quotient;
    }
 
    public static void main(String[] args)
    {
        int B = 8; // Base
        int dividend = 513; // Dividend in base B
        int divisor = 7; // Divisor in base B
 
        int quotient = divide(B, dividend, divisor);
 
        System.out.println(quotient);
    }
}


Python




import math
 
# Function to convert a number from
# base B to base 10
def toDecimal(num, B):
    decimal = 0
    power = 0
    while num != 0:
        digit = num % 10
        decimal += digit * math.pow(B, power)
        num //= 10
        power += 1
    return int(decimal)
 
# Function to convert a number
# from base 10 to base B
def fromDecimal(decimal, B):
    num = 0
    power = 0
    while decimal != 0:
        digit = decimal % B
        num += digit * math.pow(10, power)
        decimal //= B
        power += 1
    return int(num)
 
# Function to divide two numbers
# in base B
def divide(B, dividend, divisor):
    # Convert the numbers to base 10
    dividend_decimal = toDecimal(dividend, B)
    divisor_decimal = toDecimal(divisor, B)
 
    # Perform the division in base 10
    quotient_decimal = dividend_decimal // divisor_decimal
 
    # Convert the quotient to base B
    quotient = fromDecimal(quotient_decimal, B)
 
    return quotient
## This code is contributed by codearcade
# Driver code
if __name__ == "__main__":
    B = 8  # Base
 
    # Dividend in base B
    dividend = 513
 
    # Divisor in base B
    divisor = 7
 
    quotient = divide(B, dividend, divisor)
 
    print(quotient)


C#




// C# program for Division two integers
// of given base
using System;
 
public class MainClass
{
    // Function to convert a number from base B to base 10
    static int ToDecimal(int num, int B)
    {
        int _decimal = 0, power = 0;
        while (num != 0)
        {
            int digit = num % 10;
            _decimal += digit * (int)Math.Pow(B, power);
            num /= 10;
            power++;
        }
        return _decimal;
    }
 
    // Function to convert a number from base 10 to base B
    static int FromDecimal(int _decimal, int B)
    {
        int num = 0, power = 0;
        while (_decimal != 0)
        {
            int digit = _decimal % B;
            num += digit * (int)Math.Pow(10, power);
            _decimal /= B;
            power++;
        }
        return num;
    }
 
    // Function to divide two numbers in base B
    static int Divide(int B, int dividend, int divisor)
    {
        // Convert the numbers to base 10
        int dividend_decimal = ToDecimal(dividend, B);
        int divisor_decimal = ToDecimal(divisor, B);
 
        // Perform the division in base 10
        int quotient_decimal = dividend_decimal / divisor_decimal;
 
        // Convert the quotient to base B
        int quotient = FromDecimal(quotient_decimal, B);
 
        return quotient;
    }
 
    public static void Main(string[] args)
    {
        int B = 8; // Base
        int dividend = 513; // Dividend in base B
        int divisor = 7; // Divisor in base B
 
        int quotient = Divide(B, dividend, divisor);
 
        Console.WriteLine(quotient);
    }
}


Javascript




// Function to convert a number from
// base B to base 10
function toDecimal(num, B) {
    let decimal = 0;
    let power = 0;
    while (num !== 0) {
        let digit = num % 10;
        decimal += digit * Math.pow(B, power);
        num = Math.floor(num / 10);
        power++;
    }
    return decimal;
}
 
// Function to convert a number
// from base 10 to base B
function fromDecimal(decimal, B) {
    let num = 0;
    let power = 0;
    while (decimal !== 0) {
        let digit = decimal % B;
        num += digit * Math.pow(10, power);
        decimal = Math.floor(decimal / B);
        power++;
    }
    return num;
}
 
// Function to divide two numbers
// in base B
function divide(B, dividend, divisor) {
    // Convert the numbers to base 10
    let dividend_decimal = toDecimal(dividend, B);
    let divisor_decimal = toDecimal(divisor, B);
 
    // Perform the division in base 10
    let quotient_decimal = Math.floor(dividend_decimal / divisor_decimal);
 
    // Convert the quotient to base B
    let quotient = fromDecimal(quotient_decimal, B);
 
    return quotient;
}
 
// Driver code
    let B = 8; // Base
 
    // Dividend in base B
    let dividend = 513;
 
    // Divisor in base B
    let divisor = 7;
 
    let quotient = divide(B, dividend, divisor);
 
    console.log(quotient);


Output: 

57

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

Related Articles:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads