Home > Enterprise >  How do you add digits to the front of a number?
How do you add digits to the front of a number?

Time:02-04

How would you add digits to the beginning of a number (left hand side) without using a string?

I know that if you try this:

(Some psuedo code)

Let's say I try to make number 534

int current = 5;
int num = 0;

num = (num*10)  current; 

then

int current = 3;
int num = 5

num = (num*10)   current;

would make: 53

then

int current = 4;
int num = 53;

num = (num*10)   current;

would make 534

It would keep adding numbers to the right hand side of the number.

However, I am a bit confused on how you would do the opposite. How would you add numbers on the left, so instead of 534 it makes 435?

CodePudding user response:

int num = 123;
int digits = 456;

int powerOfTen = (int) Math.pow(10, (int) (Math.log10(digits)   1));

int finalNum = digits * powerOfTen   num;

System.out.println(finalNum);  // Output: 456123

The number of digits in digits is calculated using Math.log10 and Math.pow, and then used to determine the appropriate power of 10 to multiply digits by. The result is then added to num to obtain the final number with the added digits.

CodePudding user response:

Multiply the digit to add by increasing powers of 10 before summing with the current number.

int num = 0, pow = 1;
num  = 5 * pow;
pow *= 10;
num  = 3 * pow;
pow *= 10;
num  = 4 * pow; // num = 435 at this point
pow *= 10;
// ...

CodePudding user response:

An example without the use of libraries could be this:

First, get the number of digits. Then calculate the number you have to add to your initial number. The sum of these two numbers is the result you're after.

private int addNumberInFrontOf(int initialNumber, int initialNumberToAdd){
    int numberOfDigits = getDigits(initialNumber);

    int getActualNumberToAdd = getNumberToAdd(initialNumberToAdd, numberOfDigits);

    return initialNumber   getActualNumberToAdd;
}

To calculate the number of digits, you can count the number of times you can divide the initial number by 10. Notice you need to use a do-while loop because otherwise the loop wouldn't be triggered if your initial number was 0.

private int getDigits(int number) {
    int count = 0;

    do {
        number = number / 10;
        count  = 1;
    } while (number != 0);

    return count;
}

Calculate the number you need to add to your initial number by multiplying the initial number to add with the magnitude. The magnitude simply is 1 multiplied with 10 for every digit in the initial number.

private int getNumberToAdd(int number, int numberOfDigits) {
    int magnitude = 1;
    for (int i = 0; i < numberOfDigits; i  ) {
        magnitude *= 10;
    }
    return number * magnitude;
}

For example, addNumberInFrontOf(456, 123) would result in 123456. Of course, this method won't work when you use positive and negative numbers combined.

CodePudding user response:

You can use String for example.

public static int addLeft(int cur, int num) {
    return num == 0 ? cur : Integer.parseInt(cur   String.valueOf(num));
}

In case you want to avoid working with String, you can use recursion instead.

public static int addLeft(int cur, int num) {
    return num == 0 ? cur : addLeft(cur, num / 10) * 10   num % 10;
}

CodePudding user response:

You can do this, similar to how you did -

int number = 42;
int prefix = 12345;
int result = prefix * (int) Math.pow(10,String.valueOf(number).length())   number;

// Output - 1234542

CodePudding user response:

You can use some math, in python

import math
def addLeft(digit, num):
    return digit * 10 ** int(math.log10(num)   1)   num

Note that this might fail for very large numbers on account of precision issues

>>> addLeft(2, 100)
2100
>>> addLeft(3, 99)
399
>>> addLeft(6, 99999999999999)
699999999999999
>>> addLeft(5, 999999999999999)
50999999999999999  (oops)
  •  Tags:  
  • java
  • Related