Java Program to Check Armstrong Number

The concept of Armstrong numbers, also known as Narcissistic numbers, is an interesting topic in number theory and programming. These numbers are defined by a special property: when the digits of the number are raised to the power of the number of digits, and then summed up, the result is equal to the original number. Armstrong numbers are often used in programming exercises to help developers understand the fundamentals of loops, mathematical operations, and algorithms.

In this blog, we will explore how to write a Java program to check for Armstrong numbers. We’ll break down the problem, explain the logic behind the solution, and provide a detailed code example to help you understand the process. Along the way, we’ll also look at how a strong foundation in Java can make tasks like this simpler and more efficient. If you’re interested in mastering Java development, you might want to consider enrolling in a Java full stack developer course in Coimbatore, which will help you build a deeper understanding of Java and web development.

What is an Armstrong Number?

An Armstrong number (or Narcissistic number) is an integer that is equal to the sum of its own digits raised to the power of the number of digits. For example:

  • 153 is an Armstrong number because:
    13+53+33=1531^3 + 5^3 + 3^3 = 15313+53+33=153
  • 9474 is an Armstrong number because:
    94+44+74+44=94749^4 + 4^4 + 7^4 + 4^4 = 947494+44+74+44=9474
  • 370 is an Armstrong number because:
    33+73+03=3703^3 + 7^3 + 0^3 = 37033+73+03=370

The general formula for an Armstrong number of n digits is:

sum of digits raised to power of n=original number\text{sum of digits raised to power of } n = \text{original number}sum of digits raised to power of n=original number

Steps to Check for Armstrong Numbers

To determine if a number is an Armstrong number, we need to:

  1. Calculate the number of digits in the given number.
  2. Raise each digit of the number to the power of the number of digits.
  3. Sum all the results.
  4. Compare the sum with the original number. If they are the same, the number is an Armstrong number.

Writing the Java Program

Now, let’s look at how we can implement this in Java.

java

Copy

import java.util.Scanner;

public class ArmstrongNumber {

    // Function to check if a number is Armstrong

    public static boolean isArmstrong(int num) {

        int originalNum, remainder, result = 0, n = 0;

        // Assigning the number to originalNum

        originalNum = num;

        // Finding the number of digits

        while (originalNum != 0) {

            originalNum /= 10;

            ++n;

        }

        originalNum = num;

        // Checking if the sum of the nth power of each digit equals the original number

        while (originalNum != 0) {

            remainder = originalNum % 10;

            result += Math.pow(remainder, n);

            originalNum /= 10;

        }

        // If the result equals the original number, it is an Armstrong number

        return result == num;

    }

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);  

        // Taking input from the user

        System.out.print(“Enter a number to check if it is an Armstrong number: “);

        int num = scanner.nextInt();

        // Checking if the entered number is Armstrong

        if (isArmstrong(num)) {

            System.out.println(num + ” is an Armstrong number.”);

        } else {

            System.out.println(num + ” is not an Armstrong number.”);

        }

        scanner.close();

    }

}

Explanation of the Java Code

  1. Imports and Scanner Class:
    We import the Scanner class to read input from the user.
  2. isArmstrong() Function:
    • The function isArmstrong(int num) checks if a given number is an Armstrong number.
    • First, we calculate the number of digits n in the number by dividing the number by 10 until it becomes 0.
    • We then check if the sum of each digit raised to the power of n equals the original number.
    • If the result equals the original number, the function returns true; otherwise, it returns false.
  3. Main Method:
    • The main() method asks the user to input a number and calls the isArmstrong() function to check whether the number is an Armstrong number.

Output Examples

Example 1:

pgsql

Copy

Enter a number to check if it is an Armstrong number: 153

153 is an Armstrong number.

Example 2:

pgsql

Copy

Enter a number to check if it is an Armstrong number: 123

123 is not an Armstrong number.

Why is This Important?

Working with Armstrong numbers helps developers practice basic control structures like loops and conditionals. It also serves as a great way to get comfortable with fundamental mathematical operations in programming.

How to Improve Java Coding Skills for Beginners

If you are a beginner looking to improve your Java coding skills, working on problems like Armstrong numbers can help you build confidence and mastery over the basics. To further improve, here are some tips:

  1. Understand the Core Concepts: Make sure you understand the basic concepts of Java such as variables, data types, loops, conditionals, and methods before diving into complex topics.
  2. Write Code Regularly: Practice is key when learning Java. Solve coding challenges on platforms like HackerRank, LeetCode, and Codeforces.
  3. Understand Algorithms and Data Structures: Learn fundamental algorithms like sorting and searching, and data structures like arrays, lists, stacks, and queues. These are critical for solving more advanced problems.
  4. Use Debugging Tools: Learn how to use debugging tools in your IDE, like IntelliJ IDEA or Eclipse, to identify and fix bugs in your code. This is an essential skill for every programmer.
  5. Join Java Communities: Engaging with Java communities can help you learn from others, get your questions answered, and stay updated on best practices.

If you’re looking to fast-track your learning journey, consider enrolling in a Java course in Coimbatore. Java courses in Coimbatore typically offer a mix of theory and practical hands-on experience, allowing you to apply what you learn in real-world projects. These courses often include topics such as object-oriented programming, data structures, multithreading, and more, which will help you build a solid foundation in Java.

Advanced Java Concepts for Full-Stack Development

If you are interested in pursuing full-stack development, learning Java is essential. Java is widely used for back-end development, particularly with frameworks like Spring and Hibernate. Full-stack development involves both front-end (UI/UX) and back-end (server-side) development.

Java-based back-end frameworks such as Spring Boot provide a robust environment for building enterprise-level applications. By learning these technologies in-depth, you will gain the expertise needed to handle both the front-end and back-end components of an application.

By enrolling in Java full-stack training in Coimbatore, you will not only learn Java programming but also explore front-end technologies like HTML, CSS, JavaScript, and frameworks like Angular or React. Additionally, you will learn how to integrate the back-end with databases using technologies like MySQL, MongoDB, and more.

Conclusion

In this blog, we learned how to write a Java program to check for Armstrong numbers. By using loops, conditionals, and mathematical operations, we built a solution that checks whether a given number is an Armstrong number. This is a classic problem for beginners that helps strengthen basic Java concepts.

If you’re interested in becoming proficient in Java, consider enrolling in a Java full-stack developer course in Coimbatore. This course will provide you with the skills needed to work on both the back-end and front-end aspects of web development. By mastering Java and its frameworks, you’ll be prepared to build robust and scalable web applications. Whether you’re just starting out or looking to take your skills to the next level, Java full-stack training in Coimbatore is the perfect way to accelerate your learning journey.

Index