BUGSPOTTER

Armstrong Number In Java

What is an Armstrong Number ?

An Armstrong number (also called 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.

For example, a 3-digit Armstrong number is a number for which the sum of the cubes of its digits is equal to the number itself. Similarly, a 4-digit Armstrong number is a number where the sum of the fourth powers of its digits is equal to the number.

1.Steps to Check if a Number is an Armstrong Number in Java:

  1. Get the number of digits in the given number.
  2. Raise each digit to the power of the number of digits.
  3. Sum the results of these powered digits.
  4. If the sum equals the original number, then the number is an Armstrong number.

Example:

For the number 153:

1^3 + 5^3 + 3^3 = 1 +125 + 27 = 153

Since the sum (153) is equal to the original number, 153 is an Armstrong number.

Java Code to Check Armstrong Number:

				
					import java.util.Scanner;

public class ArmstrongNumber {

    // Function to check if a number is an Armstrong number
    public static boolean isArmstrong(int num) {
        int originalNum = num;  // Store the original number
        int sum = 0;
        int n = 0;
        
        // Step 1: Count the number of digits in the number
        while (num != 0) {
            num /= 10;
            n++;
        }
        
        num = originalNum; // Reset num to its original value
        
        // Step 2: Calculate the sum of digits raised to the power of n
        while (num != 0) {
            int digit = num % 10;  // Extract the last digit
            sum += Math.pow(digit, n);  // Raise it to the power of n and add to sum
            num /= 10;  // Remove the last digit
        }
        
        // Step 3: Check if sum is equal to the original number
        return sum == originalNum;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Step 4: Ask the user to input a number
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        
        // Step 5: Check if the number is an Armstrong number
        if (isArmstrong(number)) {
            System.out.println(number + " is an Armstrong number.");
        } else {
            System.out.println(number + " is not an Armstrong number.");
        }
        
        scanner.close();
    }
}

				
			

Explanation of the Java Code:

  1. isArmstrong(int num):

    • This is the method that checks if a number is an Armstrong number.
    • First, we store the original number (originalNum) to compare later.
    • We initialize a variable sum to 0, which will store the sum of each digit raised to the power of the number of digits (n).
  2. Count the number of digits (n):

    • We use a while loop to count how many digits are in the number.
    • num /= 10 removes the last digit from num by integer division.
  3. Calculate the sum of each digit raised to the power of n:

    • We extract the last digit using num % 10 (this gives the remainder when divided by 10, i.e., the last digit).
    • We then raise this digit to the power of n using Math.pow(digit, n).
    • The Math.pow function returns a double, but since we’re working with integers, this value is automatically converted to an integer during summation.
    • We remove the last digit from num using integer division (num /= 10).
  4. Compare the sum with the original number:

    • If the sum of the powered digits equals the original number, the method returns true, indicating it’s an Armstrong number; otherwise, it returns false.
  5. main method:

    • We ask the user for input and call the isArmstrong method to check if the input number is an Armstrong number.
    • Based on the result, we print whether the number is an Armstrong number or not.

 

Edge Cases to Consider:

  1. Single-digit numbers (1 to 9):

    • All single-digit numbers are Armstrong numbers because any number raised to the power of 1 is the number itself. So, 1, 2, 3, …, 9 are all Armstrong numbers.
  2. Large numbers:

    • The program can handle large numbers because Java handles large integers efficiently. However, for very large numbers, calculations might take more time due to the increased number of digits.
  3. Negative numbers:

    • The code assumes positive integers. Negative numbers are generally not considered for Armstrong number checks.

Edge Cases:

  • Single-digit numbers (1-9):
    • All single-digit numbers are Armstrong numbers because the number is equal to the sum of the number raised to the power of 1 (i.e., d1=dd^1 = d).
  • Large Numbers:
    • The program will still work for large numbers as Python handles large integers quite well.

Latest Posts

  • All Posts
  • Software Testing
  • Uncategorized
Load More

End of Content.

Categories

Enroll Now and get 5% Off On Course Fees