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

Run it Now
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("\nSmallest digit : " + sm);

    }
}

Output:

Enter a number : 5436
Smallest digit : 3