Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write two subroutines, one iterative and one recursive, that gets an unsigned in

ID: 3655357 • Letter: W

Question

Write two subroutines, one iterative and one recursive, that gets an unsigned integer as argument and returns the sum of all decimal digits in the integer. For example if the argument is 75080 then the sum to be returned is 20 (7+5+0+8+0). Write a main routine that prompts for an integer, calls these two subroutines with the input integer as argument and displays the returned value on the screen.

Explanation / Answer

import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter number: "); int num = sc.nextInt(); System.out.println("Iterative: " + iterativeSum(num)); System.out.println("Recursive: " + recursiveSum(num)); } public static int iterativeSum(int val){ int sum = 0; while(val >= 10){ sum += val%10; val = val/10; } return sum + val; } public static int recursiveSum(int val){ if(val < 10) return val; return val%10 + recursiveSum(val/10); } } rate pls