Edited Java add digits of 4 numbers -
I want to add 4 digits of 1 or 2 digits using Java.
I1 = 2 i2 = 33 i3 = 12 i4 = 10
Result: 12 (2 + 3 + 3 + 1 + 2 + 1 + 0 )
How can I do this?
Here are some pointers you can give:
1) Code> / which are integers on those arguments, then your result will also be integer
example:
int x = 7/2; Println (x);
Output: 3
(not 3.5
)
Then get rid of the last digit of the integer Everyone can, this number is divided by 10
int x = 123; X = x / 10; Println (x); There is also a modulo operator in Java that gives a reminder from the partition, like 7 / 2 = 3
and 1
will be the reminder this operator %
example
int x = 7 ; X = x% 5; Println (x);
Output: 2
because 7/5 = 1 (2 relics)
For integer you can use just like % 10
like
int x = 123; Int lastDigit = x% 10; Println (lastDigit);
Output: 3
Now try to combine this knowledge. Get the last point of the number, add it to the sum, remove this last digit (there will be no number until repeat again).
Comments
Post a Comment