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.

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("Number is armstrong:" + n);
        } else {
            System.out.println("Number is not armstrong:" + n);
        }

    }
}

Output:

Enter a number:153
Number is armstrong:153