Write a Java Program to Show the Value of the Hundreds Place, the Tens Place, and the Ones Place for a Three-Digit Number

How to get the 'place' of a Number in Java (eg. tens, thousands, etc)

You can use logarithms.

    double[] values = {4, 77, 234, 4563, 13467, 635789};
for(int i = 0; i < values.length; i++)
{
double tenthPower = Math.floor(Math.log10(values[i]));
double place = Math.pow(10, tenthPower);
System.out.println(place);
}

How to get the hundreds place value from an integer value?

Let me guess, you probably want something along these lines:

if (first / 100 == second / 100 && first / 100 == third / 100) {
System.out.println(first / 100);
System.out.println(second / 100);
System.out.println(third / 100);
}

This will print the same value three times if the condition is true, though, which may not be exactly what you want. For example, if the numbers are 756, 723 and 792, it will print 7 three times. Anyway, you can try it out, see if you can modify it to your needs, and if not, explain where I guessed wrong.

Edit: to reduce the number of divisions you can alternatively just divide all the values by 100 before the if statement:

first /= 100; // or if you prefer: first = first / 100;
second /= 100;
third /= 100;
if (first == second && first == third) {
System.out.println(first);
System.out.println(second);
System.out.println(third);
}

Java:Three digit Sum - Find out all the numbers between 1 and 999 where the sum of 1st digit and 2nd digit is equal to 3rd digit

Don't know what you're trying to do with that while loop, or why you are building up a space-separated string of numbers.

Your code should be something like:

for (int n = 1; n <= 999; n++) {
int digit1 = // for you to write code here
int digit2 = // for you to write code here
int digit3 = // for you to write code here
if (digit1 + digit2 == digit3) {
// print n here
}
}

I need to pull digit places (like tenths place, hundredths place, etc...)

You can change your constructor to this:

public LcdDigit (int newDigitValue, int place) {
if (newDigitValue > 0) {
digitValue = newDigitValue;
}

if (place == 1) {
digitValue = digitValue % 10;
} else if (place == 10) {
digitValue = (digitValue / 10) % 10;
} else {
digitValue = (digitValue / 100) % 10;
}

}

You will also have to change your setDigitValue method to something similar.

This works by moving the digit that you are looking for to the one's place, which is then easily found by using the modulus operator. For example, if your digit value is 230 and you're trying to find the ten's place you would first divide by 10. The resulting value is 23. 23 % 10 equals 3, which is the digit in the ten's place of the original value.

Getting place values of a number w/ modulus?

        var ones = Math.floor(num % 10),
tens = Math.floor(num/10 % 10),
hundreds = Math.floor(num/100 % 10),
thousands = Math.floor(num/1000 % 10),
tenThousands = Math.floor(num / 10000 % 10),
hundredThousands = Math.floor(num / 100000 % 10),
millions = Math.floor(num / 1000000 % 10),
tenMillions = Math.floor(num / 10000000 % 10),
hundredMillions = Math.floor(num / 100000000 % 10);

Managed to get to 100 million.



Related Topics



Leave a reply



Submit