Complete the implementation of method sumOfDigits(int num), return the sum of its digits recursively. Consider using mod

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Complete the implementation of method sumOfDigits(int num), return the sum of its digits recursively. Consider using mod

Post by answerhappygod »

Complete the implementation of method sumOfDigits(int num),
return the sum of its digits recursively. Consider using mod(%)
operator to get the right-most digit in your solution.
Sample runs
Input 1:
Output 1:
Input 2:
Output 2:
Given Code
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
while (stdin.hasNextInt()) {

System.out.println(sumOfDigits(stdin.nextInt()));
}
stdin.close();
}
public static int sumOfDigits(int n) {
// your code starts here
// your code ends here
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply