In  this tutorial, we will see “How to check if a number is an Armstrong Number using Java?”

Check if number is Armstrong number in java

Java – How to check if number is an Armstrong Number? Share on X

/**
 * Check Armstrong Number in Java
 * @author Deepak Verma
 *
 */

public class Check_If_Number_is_Armstrong_Number_Java8_Example {
    
    public static void main(String[] args) {
        
    	int number = 250;
        
    	if (isArmstrongNumber(number))
            System.out.println(number + " is an Armstrong number.");
        else
            System.out.println(number + " is not an Armstrong number.");
    
    }
    
    public static boolean isArmstrongNumber(int number) {
    
    	int originalNumber = number;
        int result = 0;
        int n = String.valueOf(number).length();
        
        while (number != 0) {
            int digit = number % 10;
            result += Math.pow(digit, n);
            number /= 10;
        }
        
        return originalNumber == result;
    }
}

 

Output:

250 is not an Armstrong number.

check if text or string present in a file using java 8

skip method java 8 stream

Java – How to check if number is an Armstrong Number? Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts