Kodekraftt
/Blog
/Armstrong Number In Java Explained With Examples
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Armstrong Number in Java – Explained with Examples

A detailed guide on Armstrong Numbers in Java. Learn what an Armstrong number is, how it works, and how to write efficient Java programs to check Armstrong numbers using loops and mathematical logic. Includes clear explanations, examples, and optimized Java code.

Armstrong Number in Java

Last updated: February 8, 2025

An Armstrong Number (also known as a Narcissistic Number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. In this blog, we will understand the concept and learn how to check Armstrong numbers using Java.

What is an Armstrong Number?

A number with n digits is an Armstrong number if:

sum = digit₁ⁿ + digit₂ⁿ + digit₃ⁿ + ... + digitₙⁿ
  

If the sum equals the original number, it is an Armstrong number.

Examples

  • 153 → 1³ + 5³ + 3³ = 153
  • 370 → 3³ + 7³ + 0³ = 370
  • 9474 → 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474

Java Program to Check Armstrong Number

Below is a simple Java program using loops and math logic:


import java.util.Scanner;

public class ArmstrongNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        int original = num;
        int sum = 0;

        // count digits
        int digits = String.valueOf(num).length();

        while (num > 0) {
            int digit = num % 10;
            sum += Math.pow(digit, digits);
            num /= 10;
        }

        if (sum == original) {
            System.out.println(original + " is an Armstrong Number.");
        } else {
            System.out.println(original + " is NOT an Armstrong Number.");
        }
    }
}
  

Optimized Version

A slightly optimized solution that avoids converting the number to string:


public static boolean isArmstrong(int num) {
    int digits = 0, temp = num;

    // count digits
    while (temp > 0) {
        temp /= 10;
        digits++;
    }

    int sum = 0;
    temp = num;

    while (temp > 0) {
        int digit = temp % 10;
        sum += Math.pow(digit, digits);
        temp /= 10;
    }

    return sum == num;
}
  

Conclusion

Armstrong numbers are a classic programming exercise useful for practicing loops, conditionals, and mathematical logic in Java. Try modifying the program to find Armstrong numbers in a range or generate all Armstrong numbers up to 10000.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.