Java Program to check a number is Armstrong or not

In this program, You will learn how to check a number is Armstrong or not in java.


class Main {
   //statement
}

Java Program to check a number is Armstrong or not

Example: How to check a number is Armstrong or not in java.

import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        int n, t, r, rev = 0;

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number:");
        n = sc.nextInt();
        t = n;

        while (n > 0) {
            r = n % 10;
            rev = rev + r * r * r;
            n = n / 10;
        }

        if (rev == t){
            System.out.println("Number is ArmStrong:" + t);
        }else{
            System.out.println("Number is not ArmStrong:" + t);
        }

    }
}

Output:

Enter a number:153
Number is ArmStrong:153