+ 4
I need help! Java
How can I do a program to read a number like 7654 and the print to: Units: 4 Tens: 5 Hundreds: 6 Thousands: 7
8 ответов
+ 20
● use %10 to take out last digit & use /10 to remove units place
Example: 7654%10 = 4 //units
7654/10 = 765
765%10 = 5 //tens
765/10 = 76
76%10 = 6 //hundreds
76/10 = 7
7%10 = 7 //thousands
● run a loop for that for (number of digits time) or till N becomes 0 by /10
● you can use array for keeping "units", "tens" etc & use it while printing output.
+ 13
Because why not...
https://code.sololearn.com/cwJ193LCMF7n/?ref=app
+ 11
In pseudocode you could take the num in as an int then run if statements on it. Assign int variables on tens, hundreds, thousands, units then return those variables If num >=1000 print thousands:1; hundreds;0 tens ;0 etc. Else if num<1000 print hundreds, tens. Else if num<100 print tens, units. Else if nums <10 print units
+ 11
Yep J 12323123 it's not really robust. Ints only ;) But should be enough to get the idea.
+ 10
Thanks Tashi N I suppose all thats missing on your code is some try catch error statements for incorrect input
+ 5
I would do like this:
String num = "1595";
char[] nums = num.toCharArray();
String[] notation = {"thousands","hundreds","tens","units"};
int i = nums.length - 1;
for(;i >= 0; i--) {
System.out.println(nums[i] + " " + notation[i]);
}
I did an extended version too, including Input request and exception treatment: https://code.sololearn.com/chT3Je2RIOzx
+ 3
simple version
https://code.sololearn.com/cl9AfnZh5g3L/#java
+ 2
احب