DEV Community

PONVEL M
PONVEL M

Posted on

Recursion Programs: Sum of Digits, Reverse Number & Digit Printing (Python, Java, JavaScript)

Recursion is one of the most important concepts in programming. It means a function calls itself to solve smaller parts of a problem.

In this blog, we will understand recursion with three simple and important problems:

  • Sum of digits of a number
  • Reverse a number
  • Print digits of a number

We will implement all these in Python, Java, and JavaScript.


1. Sum of Digits Using Recursion

Problem

Find the sum of digits of a number.

Example:
Input: 1234
Output: 10


Logic

  • Take last digit → n % 10
  • Remove last digit → n / 10
  • Repeat until number becomes 0

Python Code

def sum_digits(n):
    if n == 0:
        return 0
    return n % 10 + sum_digits(n // 10)

print(sum_digits(1234))
Enter fullscreen mode Exit fullscreen mode

Java Code

class Main {
    static int sumDigits(int n){
        if(n == 0) return 0;
        return n % 10 + sumDigits(n / 10);
    }

    public static void main(String[] args){
        System.out.println(sumDigits(1234));
    }
}
Enter fullscreen mode Exit fullscreen mode

JavaScript Code

function sumDigits(n){
    if(n === 0) return 0;
    return n % 10 + sumDigits(Math.floor(n / 10));
}

console.log(sumDigits(1234));
Enter fullscreen mode Exit fullscreen mode

2. Reverse a Number Using Recursion

Problem

Reverse a number using recursion.

Example:
Input: 1234
Output: 4321


Logic

  • Take last digit
  • Build reversed number step by step

Python Code

def reverse(n, rev=0):
    if n == 0:
        return rev
    return reverse(n // 10, rev * 10 + n % 10)

print(reverse(1234))
Enter fullscreen mode Exit fullscreen mode

Java Code

class Main {
    static int reverse(int n, int rev){
        if(n == 0) return rev;
        return reverse(n / 10, rev * 10 + n % 10);
    }

    public static void main(String[] args){
        System.out.println(reverse(1234, 0));
    }
}
Enter fullscreen mode Exit fullscreen mode

JavaScript Code

function reverse(n, rev = 0){
    if(n === 0) return rev;
    return reverse(Math.floor(n / 10), rev * 10 + n % 10);
}

console.log(reverse(1234));
Enter fullscreen mode Exit fullscreen mode

3. Print Digits of a Number

Problem

Print each digit of a number.

Example:
Input: 1234

Output:
1
2
3
4


Logic

  • First go to the last digit using recursion
  • Print while returning back

Python Code

def print_digits(n):
    if n == 0:
        return
    print_digits(n // 10)
    print(n % 10)

print_digits(1234)
Enter fullscreen mode Exit fullscreen mode

Java Code

class Main {
    static void printDigits(int n){
        if(n == 0) return;
        printDigits(n / 10);
        System.out.println(n % 10);
    }

    public static void main(String[] args){
        printDigits(1234);
    }
}
Enter fullscreen mode Exit fullscreen mode

JavaScript Code

function printDigits(n){
    if(n === 0) return;
    printDigits(Math.floor(n / 10));
    console.log(n % 10);
}

printDigits(1234);
Enter fullscreen mode Exit fullscreen mode

Conclusion

Recursion is a powerful concept that helps us solve problems in a clean and simple way.

✔ Breaks problems into smaller parts
✔ Makes code more readable
✔ Useful for mathematical and logical problems


Final Thought

If you understand these three problems, you can easily solve many recursion-based questions in interviews.

Practice more to master recursion!


Thank you for reading!

Top comments (0)