-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
32 lines (24 loc) · 695 Bytes
/
Main.java
File metadata and controls
32 lines (24 loc) · 695 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
public class Main {
public static void main(String[] args) {
int number = 584;
System.out.println("Sum of digits of " + number + " is " + sumDigits(number));
System.out.println(sumDigits(1234));
System.out.println(sumDigits(-125));
System.out.println(sumDigits(4));
System.out.println(sumDigits(32123));
}
public static int sumDigits(int number){
if (number<0){
return -1;
}
if (number<=9){
return number;
}
int sumOfDigits = 0;
while(number!=0){
sumOfDigits += number%10;
number /= 10;
}
return sumOfDigits;
}
}