Java Program to check a number is Armstrong or not using recursion


In this program, you will learn how to check a number is Armstrong or not using recursion in Java.


Some list of armstrong numbers : 153 370 371 407


Example: How to check a number is Armstrong or not using recursion in Java

Run it Now
import java.util.Scanner;

class Main {
    int rev = 0, r;

    int reverseNumber(int n) {
        if (n > 0) {
            r = n % 10;
            rev = rev + r * r * r;
            reverseNumber(n / 10);
        }
        return rev;
    }

    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 rev = obj.reverseNumber(n);

        if (rev == n) {
            System.out.println("\nNumber is armstrong " + n);
        } else {
            System.out.println("\nNumber is not armstrong " + n);
        }

    }
}

Output:

Enter a number : 407
Number is armstrong 407