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.


Some list of armstrong numbers is : 153, 370, 371, 407


Example: How to check a number is armstrong or not in Java

Run it Now
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("\nThe Number is ArmStrong :" + t);
        }else{
          System.out.println("\nThe Number is Not ArmStrong :" + t);
        }

    }
}

Output:

Enter a Number :153
The Number is ArmStrong :153