We Are Going To Discuss About write a Java program to show the value of the hundreds place, the tens place, and the ones place for a three-digit number. So lets Start this Java Article.
write a Java program to show the value of the hundreds place, the tens place, and the ones place for a three-digit number
- write a Java program to show the value of the hundreds place, the tens place, and the ones place for a three-digit number
Replace
System.out.printf("Hundreds place digit: " , hundreds);
withSystem.out.println("Hundreds place digit: " + hundreds);
and remove the;
after theif
-statments. - write a Java program to show the value of the hundreds place, the tens place, and the ones place for a three-digit number
Replace
System.out.printf("Hundreds place digit: " , hundreds);
withSystem.out.println("Hundreds place digit: " + hundreds);
and remove the;
after theif
-statments.
Solution 1
We can get ones, hundred or any other place value in Java using a simple code as shown below:
int n=356;
int one=(n/1)%10;
int tens= (n/10)%10;
int hundred = (n/100)%10;
Original Author Pooja Of This Content
Solution 2
You have little error of calculating tenth and ones digit.
//Displays tens digit
tens = (number %100) / 10;
System.out.println("Tens place digit: " + tens);
//Display ones digit
ones = number %10;
System.out.println("Ones place digit: " ,+ ones);
Remove semicolons after if conditions.
Again, the 3 digit checking should be done just after reading the number. Otherwise pointless calculation will be done for invalid numbers.
Original Author Shahid Of This Content
Solution 3
Replace System.out.printf("Hundreds place digit: " , hundreds);
with System.out.println("Hundreds place digit: " + hundreds);
and remove the ;
after the if
-statments.
If you want to use printf
take a look at this: https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object…)
Original Author sinclair Of This Content
Solution 4
Completely Fixed code is here :
You can compare with your code to find error in your code
import java.util.Scanner;
class ValueOfDigits {
public static void main(String[] args)
{
//Create new scanner
Scanner input = new Scanner(System.in);
//Values of each digit
int hundreds = 0;
int tens = 0;
int ones = 0;
//Prompt user to input 3 digit number
System.out.print("Enter a 3 digit number: ");
int number = input.nextInt();
if (number <= 999 && number > 99) // Checking condition for three digit number
{
//Displays hundreds place digit
hundreds = number / 100;
System.out.printf("Hundreds place digit: " + hundreds);
//Displays tens digit
tens = (number - (hundreds*100)) / 10; // compare with your code
System.out.printf("\nTens place digit: " + tens);
//Display ones digit
ones = (number - (tens*10) - (hundreds*100)); // compare with your code
System.out.printf("\nOnes place digit: " + ones);
}
//Error if number is less or more then three digits
else
{
if (number > 999)
System.out.println("\nError! Number more then 3 digits.");
if (number < 100)
System.out.println("Error! Number less then 3 digits.");
}
}
}
Original Author Peter Rahul Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.