How to Check if Given Number Is Armstrong Number in Java


In this example, let’s see how to check if the given number is Armstrong number or not in Java.

Source Code

package com.beginner.examples;

public class ArmstrongExample {
     
    public static void main(String[] args){
    	int sum = 0, a, num;
    	// It is the number to be checked.
        int number = 153;
        num = number;
        while (number > 0) {
            a = number % 10;
            number = number / 10;
            sum = sum + (a * a * a);
        }
        if (num == sum)
            System.out.println("153 is an armstrong number.");
        else
            System.out.println("153 is not armstrong number");
      }
}

Output:

153 is an armstrong number.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments