-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathDigits.java
More file actions
36 lines (31 loc) · 888 Bytes
/
Digits.java
File metadata and controls
36 lines (31 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Write a program to extract each digit from an int, in the reverse order.
*
* For example, if the int is 1542, the output shall be "2,4,5,1",
* with a comma separating the digits
*/
package javaexercises.flowcontrol;
/**
*
* @author User
*/
public class Digits {
public static void main(String[] args) {
Digits aDigits = new Digits();
aDigits.printDigits(1542, ',');
}
private void printDigits(long number, char separator)
{
long digit;
System.out.printf("Digits from number %1$d in the reverse order are:\n", number);
do{
digit = number % 10;
number = number / 10;
System.out.print(digit);
if (number > 0) {
System.out.print(separator);
}
} while(number > 0);
System.out.println();
}
}