Menu
Question Index
...

What will be the output of the following program?

public class SumOfDigit {
    public static void main(String args[]) {
        int x = sumOfDigits(1234567);
        int y = sumOfDigits(97842132);
        int z = sumOfDigits(254671524);
        int sum = sumOfDigits(x + y + z);
        System.out.println("Sum Of Digits " + sum);
    }
    public static int sumOfDigits(int number) {
        if (number / 10 == 0)
            return number;
        return number % 10 + sumOfDigits(number / 10);
    }
    public static int sumOfDigitsIterative(int number) {
        int result = 0;
        while (number != 0) {
        result = result + number % 10;
            number = number / 10;
        }
        return result;
    }
}


Sum Of Digits 1
Sum Of Digits 3
Sum Of Digits 9
Sum Of Digits 5
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Recursive In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App