Sample Program:-
public class SampleProgram {
static int sum = 0;
public static void main(String[] args) {
int number = 1234;
System.out.println("sum of digits : " + sumOfDigits(number));
}
private static int sumOfDigits(int number) {
if (number == 0) {
return sum;
} else {
sum = sum + (number % 10);
sumOfDigits(number / 10);
}
return sum;
}
}
Output:-
sum of digits : 10
Enjoy Coding.
No comments:
Post a Comment