Edited Java add digits of 4 numbers [closed]

Here are some hints you may need:

1) If you use / on arguments which will be integers then your result will also be integer.

Example:

int x = 7/2;
System.out.println(x);

output: 3 (not 3.5)

So to get rid of last digit of integer all you can do is divide this number by 10 like

int x = 123;
x = x/10;
System.out.println(x);

Output: 12

2) Java also have modulo operator which returns reminder from division, like 7/2=3 and 1 will be reminder. This operator is %

Example

int x = 7; 
x = x % 5;
System.out.println(x);

Output: 2 because 7/5=1 (2 remains)

So to get last digit from integer you can simply use % 10 like

int x = 123;
int lastDigit = x%10;
System.out.println(lastDigit);

Output: 3

Now try to combine this knowledge. Get last digit of number, add it to sum, remove this last digit (repeat until will have no more digits).

Leave a Comment