Java Program to find the smallest digit of a number using recursion


In this program, you will learn how to find the smallest digit of a number using recursion in java.


123 => 1

9824 => 2

Example: How to find the smallest digit of a number using recursion in java.

import java.util.Scanner;

class Main {
    int sm = 9, r;


    int largestDigit(int n) {

        if (n > 0) {
            r = n % 10;
            if (sm > r)
                sm = r;
            largestDigit(n / 10);
        }

        return sm;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number:");
        int n = sc.nextInt();

        Main obj = new Main();
        int sm = obj.largestDigit(n);

        System.out.println("Smallest digit:" + sm);

    }
}

Output:

Enter a number:3256
Smallest digit:2