+ 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

3rd Mar 2019, 1:28 AM
Christian Santiago
Christian Santiago - avatar
9 Answers
+ 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.
3rd Mar 2019, 5:35 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
5th Mar 2019, 10:37 PM
Tashi N
Tashi N - avatar
+ 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
3rd Mar 2019, 3:06 AM
J 12323123
J 12323123 - avatar
+ 11
Yep J 12323123 it's not really robust. Ints only ;) But should be enough to get the idea.
5th Mar 2019, 10:53 PM
Tashi N
Tashi N - avatar
+ 10
Thanks Tashi N I suppose all thats missing on your code is some try catch error statements for incorrect input
5th Mar 2019, 10:51 PM
J 12323123
J 12323123 - avatar
+ 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
7th Mar 2019, 12:54 AM
Rafael S Valle
Rafael S Valle - avatar
7th Mar 2019, 10:58 AM
zemiak
+ 2
ۭۧۚ
6th Mar 2019, 4:34 PM
ŰčÙ…Ű§ŰŻ Ű§Ù„ŰŻÙŠÙ†
ŰčÙ…Ű§ŰŻ Ű§Ù„ŰŻÙŠÙ† - avatar