Recursive Digit Sum

  • + 7 comments

    This is not a case for recursion at all. The below code in Java will solve without any recursion being used.

    import java.math.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            String arr[] = new Scanner(System.in).nextLine().split("[ ]");
            int output = (Integer.parseInt(arr[1])*new BigInteger(arr[0]).remainder(new BigInteger("9")).intValue())%9;
            System.out.println(output == 0 ? 9:output);
        }
    }