What's the answer for "Sum of Digits" in Java ?
Posted by: 1 | Subject: Programming | Date: 2024-10-06 13:26:33
import java.util.*;
public class Main {
public static int sumOfDigits(String n) {
if (n.length() == 1) {
return Character.getNumericValue(n.charAt(0));
}
return Character.getNumericValue(n.charAt(0)) + sumOfDigits(n.substring(1));
}
public static void main(String[] args) {
// Enter your code here
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
// Calculate and print the sum of digits using recursion
int result = sumOfDigits(input);
System.out.println(result);
scanner.close();
}
}
Date: 2024-10-06 13:26:57
Log in to post an answer.
What is 10 - 6 ?
Posted by: 1 | Subject: Maths | Date: 2024-09-23 09:16:45
Log in to post an answer.
What is Al ?
Posted by: 1 | Subject: Chemistry | Date: 2024-08-14 16:37:50
Log in to post an answer.