Open In App

JavaScript Program to Divide Large Number Represented as String

In this article, we will explore dividing large numbers represented as strings in JavaScript. Dividing a large number represented as a string in JavaScript is a common computational task that involves dividing a numerical line by another numerical value.

Approach 1: Brute Force

We will use basic maths, as the dividend and result can be huge we store them in string. We first take digits which are divisible by a number. After this take each digit and store the result in a string.



Syntax:

function divide(dividend, divisor) {
// Implementation
return result;
}

Example: Below is the implementation of the program.






function divide(dividend, divisor) {
  
    // Initialize an empty 
    // string to store the result
    if (divisor === 0) {
        throw new Error(
            "Division by zero is not allowed."
        );
    }
    let quotient = "";
      
    // Initialize variables for 
    // index and temporary storage
    let currentIndex = 0;
    let currentDividendPart =
        dividend[currentIndex] - "0";
          
    /* Find the first part of the dividend that is 
       greater than or equal to the divisor */
    while (currentDividendPart < divisor) {
        currentDividendPart =
            currentDividendPart * 10 +
            dividend[currentIndex + 1].
                charCodeAt(0) - "0".charCodeAt(0);
        currentIndex += 1;
    }
    currentIndex += 1;
      
    // Perform long division
    while (dividend.length > currentIndex) {
      
        // Store the result of 
        // the division in the quotient
        quotient += String.fromCharCode(
            Math.floor(currentDividendPart / divisor)
            + "0".charCodeAt(0)
        );
  
        // Calculate the next remainder 
        // and digit of the dividend
        currentDividendPart =
            (currentDividendPart % divisor) * 10 +
            dividend[currentIndex].charCodeAt(0) -
            "0".charCodeAt(0);
        currentIndex += 1;
    }
    quotient += String.fromCharCode(
        Math.floor(currentDividendPart / divisor)
        + "0".charCodeAt(0)
    );
  
    // If the result is empty, return "0"
    if (quotient.length === 0) {
        return "0";
    }
      
    // Otherwise, return the result as a string
    return quotient;
}
  
// Driver Code
let dividend = "1322145464651";
let divisor = 125;
console.log(divide(dividend, divisor));

Output
10577163717

Appraoch 2: Using BigInt() in JavaScript

Syntax:

const bigint1 = BigInt(num1);
const bigint2 = BigInt(num2);

Example: Below is the implementation of the above approach.




function divideLargeNumbers(num1, num2) {
  
    // Convert the input strings to BigInt
    const bigint1 = BigInt(num1);
    const bigint2 = BigInt(num2);
  
    // Check if num2 is zero (division by zero)
    if (bigint2 === 0) {
        throw new Error("Division by zero is not allowed.");
    }
  
    // Perform the division
    const result = bigint1 / bigint2;
  
    // Convert the result back to a string
    return result.toString();
}
  
// Example usage:
const num1 = "1322145464651";
const num2 = "125";
const result = divideLargeNumbers(num1, num2);
console.log(result);

Output
10577163717

Approach 3: Using parseInt and toString:

Syntax:

const number1 = parseInt(num1, 10);
const number2 = parseInt(num2, 10);

Example: Below is the implementation of the above approach.




function divideLargeNumbers(num1, num2) {
  
    // Parse the input strings to numbers
    const number1 = parseInt(num1, 10);
    const number2 = parseInt(num2, 10);
  
    // Check if num2 is zero (division by zero)
    if (number2 === 0) {
        throw new Error("Division by zero is not allowed.");
    }
  
    // Perform the division
    const result = number1 / number2;
  
    // Convert the result back to a string
    return result.toString().split('.')[0];
}
  
// Example usage:
const num1 = "1322145464651";
const num2 = "125";
const result = divideLargeNumbers(num1, num2);
console.log(result);

Output
10577163717

Article Tags :