๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

WAP to check whether a number is an Armstrong number in Java  


What is Armstrong Number ?
  • An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of the n-th power of its digits, where n is the number of digits.
  • Example: 153 is an Armstrong number because:
    13+53+33=153
Logical Steps:
  • Take one number.
  • Initialize a sum variable to 0 and store the original number in a temporary variable.
  • Use a loop:
    • Extract the last digit using no % 10.
    • Calculate the cube of the digit and add it to the sum.
    • Remove the last digit using no = no / 10.
  • Repeat the process until the number becomes 0.
  • Check if the sum is equal to the original number.
    • If true, it is an Armstrong number; otherwise, it is not.
Programs:
  • Below is the simple program:
    public class CheckArmstrongNumber
    {
        public static void main(String[] args)
        {
            int no = 153;
            int originalNumber = no;
            int sum = 0;
    
            // Calculate the number of digits
            int temp = no;
            int digits = 0;
            while (temp != 0)
            {
                digits++;
                temp = temp / 10;
            }
    
            while (no != 0)
            {
                int rem = no % 10;
                int mul = 1;
    
                // Calculate rem^digits using a basic loop
                for (int i = 0; i < digits; i++)
                {
                    mul = mul * rem;
                }
    
                sum = sum + mul;
                no = no / 10;
            }
    
            if (sum == originalNumber)
            {
                System.out.println(originalNumber + " is an Armstrong number.");
            }
            else
            {
                System.out.println(originalNumber + " is not an Armstrong number.");
            }
        }
    }
    Output:
    153 is an Armstrong number.
  • Below is the program by taking user input:
    import java.util.Scanner;
    
    public class CheckArmstrongNumber
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
    
            System.out.print("Enter a number: ");
            int no = scanner.nextInt();
    
            int originalNumber = no;
            int sum = 0;
    
            // Calculate the number of digits
            int temp = no;
            int digits = 0;
            while (temp != 0)
            {
                digits++;
                temp = temp / 10;
            }
    
            while (no != 0)
            {
                int rem = no % 10;
                int mul = 1;
    
                // Calculate rem^digits using a basic loop
                for (int i = 0; i < digits; i++)
                {
                    mul = mul * rem;
                }
    
                sum = sum + mul;
                no = no / 10;
            }
    
            if (sum == originalNumber)
            {
                System.out.println(originalNumber + " is an Armstrong number.");
            }
            else
            {
                System.out.println(originalNumber + " is not an Armstrong number.");
            }
        }
    }
    Output:
    Enter a number: 1634
    1634 is an Armstrong number.
Program Explanation
  • Take input from the user using Scanner and store it in an integer variable no.
    Example: no = 1634
  • Initialize a variable sum to 0 to keep track of the sum of the multiplication digits.
    int sum = 0;
  • Calculate the number of digits in the number using a while loop:
    • Use a temporary variable temp to avoid modifying the original number.
    • Divide the number by 10 in each iteration and increment the digits counter until the number becomes 0.
      Example: For 1634, the number of digits is 4.
  • Use another while loop that runs until no becomes 0:
    • Extract the last digit using no % 10.
      Example: For 1634, the last digit is 4.
    • Calculate the power of the digit raised to the number of digits using a for loop
      • Multiply the digit by itself digits times.
        Example: 44=25644=256
    • Add the power to the sum:
      Example: sum = 0 + 256 = 256
    • Remove the last digit from no by dividing it by 10.
      Example: 1634 becomes 163.
  • Repeat the process until all digits are processed.
    Example:
    34=81, 64=1296, 14=1
    The sum becomes 256 + 81 + 1296 + 1 = 1634.
  • Compare sum with the original number (originalNumber)
    • If they are equal, the number is an Armstrong number.
    • Otherwise, it is not an Armstrong number.
  • Print the result using System.out.println().
  • Close the scanner with scanner.close() to avoid resource leaks.