Introduction
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. For example, 153 is an Armstrong number because (1^3 + 5^3 + 3^3 = 153). This guide will show you how to create a Java program to check whether a given number is an Armstrong number.
Problem Statement
Create a Java program that takes an integer input from the user and checks if the number is an Armstrong number.
Example 1:
- Input:
153
- Output:
153 is an Armstrong number.
Example 2:
- Input:
9474
- Output:
9474 is an Armstrong number.
Example 3:
- Input:
123
- Output:
123 is not an Armstrong number.
Solution Steps
- Take Input: Get the number from the user.
- Calculate the Number of Digits: Determine the number of digits in the input number.
- Compute the Sum of Digits Raised to Power: Raise each digit to the power of the total number of digits and compute the sum.
- Check for Armstrong Number: Compare the computed sum to the original number.
- Output the Result: Display whether the number is an Armstrong number.
Java Program
import java.util.Scanner;
/**
* Java Program to Check Armstrong Number
* Author: https://www.javaguides.net/
*/
public class ArmstrongNumberChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Take input from the user
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Step 2: Calculate the number of digits
int numberOfDigits = String.valueOf(number).length();
// Step 3: Compute the sum of digits raised to the power of numberOfDigits
int sum = 0;
int temp = number;
while (temp > 0) {
int digit = temp % 10;
sum += Math.pow(digit, numberOfDigits);
temp /= 10;
}
// Step 4: Check if the sum is equal to the original number
if (sum == number) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}
}
Step by Step Explanation
Take Input: The program starts by prompting the user to enter a number.
Calculate the Number of Digits: The
String.valueOf(number).length()
is used to determine the number of digits in the input number. For example, if the input is 153, the number of digits is 3.Compute the Sum of Digits Raised to Power:
- A temporary variable
temp
is used to store the original number. - The program iterates through each digit of the number by using the modulus operator
%
and integer division/
. - For each digit, the program raises it to the power of
numberOfDigits
and adds the result tosum
. - The loop continues until all digits have been processed.
- A temporary variable
Check for Armstrong Number: After the loop, the sum of the digits raised to their respective powers is compared to the original number.
Output the Result: If the sum equals the original number, it is an Armstrong number. Otherwise, it is not.
Output Examples
Example 1:
Enter a number: 153
153 is an Armstrong number.
Example 2:
Enter a number: 9474
9474 is an Armstrong number.
Example 3:
Enter a number: 123
123 is not an Armstrong number.
Conclusion
This Java program effectively checks whether a given number is an Armstrong number. The key steps involve calculating the number of digits, raising each digit to the power of the number of digits, summing these values, and comparing the sum to the original number. This program can be easily extended to check Armstrong numbers of different lengths or for different bases.
Related Java Programs with Output
- Java Program to Find Factorial Using Recursion
- Java Program to Find Sum of Natural Numbers
- Java Program to Reverse a Number
- Java Program to Find Prime Numbers
- Java Program to Check Armstrong Number
- Java Program to Count the Number of Digits in a Number
- Java Program for Prime Numbers Within a Range
- Java Program to Find the Largest of Three Numbers
- Java Program to Check if a Number is Prime
- Java Program to Find the Fibonacci Series
- Java Program to Check if a Number is Positive or Negative
- Java Program to Find GCD of Two Numbers
- Java Program to Find Strong Number
- Java Program to Find LCM of Two Numbers
- Java Program to Check Palindrome Number
- Java Program to Swap Two Numbers Without Using a Temp Variable
- Java Program to Find the Second Largest Number in an Array
- Java Program to Find GCD of Two Numbers
- Java Program to Find Strong Number
- Java Program to Find LCM of Two Numbers
- Java Program to Check Palindrome Number
- Java Program to Find the Largest of Three Numbers
- Java Program to Check if a Number is Prime
- Java Program to Swap Two Numbers Without Using a Temp Variable
- Java Program to Find the Second Largest Number in an Array
- Java Program to Check if a Given Number is Perfect Square
- Java Program to Find the Fibonacci Series
- Java Program to Check if a Number is Positive or Negative
Comments
Post a Comment
Leave Comment