
Introduction
An Armstrong number (also known as a narcissistic number) is a number that equals the sum of its own digits each raised to the power of the number of digits. For instance, 153 is an Armstrong number because (1^3 + 5^3 + 3^3 = 153). Exploring Armstrong numbers between two intervals can be a common task in introductory Java programming courses, providing a great way to learn about loops, conditionals, and functions.
In this article, you will learn how to create a Java program that displays Armstrong numbers between two intervals. The explanations, followed by code examples, will guide you through writing a function that checks for Armstrong numbers, and then using that function to find and display them within a specified range.
Defining the Armstrong Number Check Function
Outline of the Function
- Define a function that takes an integer as input.
- Compute the number of digits in the integer.
- Sum the powers of each digit according to the number of digits.
- Compare the sum to the original number and return a boolean value.
Implementation of the Function
Utilize Java's mathematical operations to break down the number into digits.
Calculate the sum of the digits each raised to the power of the total number of digits.
Return
true
if the sum equals the original number, otherwise returnfalse
.javapublic static boolean isArmstrong(int number) { int originalNumber, remainder, result = 0, n = 0; originalNumber = number; for (;originalNumber != 0; originalNumber /= 10, ++n); originalNumber = number; for (;originalNumber != 0; originalNumber /= 10) { remainder = originalNumber % 10; result += Math.pow(remainder, n); } return (result == number); }
In this code:
- The first loop counts the digits in the number.
- The second loop calculates the sum of each digit raised to the power of the number of digits.
Implementing the Main Method to Display Armstrong Numbers
Setting Up the Main Method
- Read the lower and upper limits for the interval from the user.
- Loop through the interval using a
for
loop. - Use the function
isArmstrong()
to check each number, and print it if it is an Armstrong number.
Code for Main Method
Create a Scanner object to capture user input.
Promote good user interaction by providing clear prompts.
Iterate through the desired range, and use the function to check for Armstrong numbers.
javaimport java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int low, high; System.out.print("Enter the lower bound: "); low = scanner.nextInt(); System.out.print("Enter the upper bound: "); high = scanner.nextInt(); System.out.println("Armstrong numbers between " + low + " and " + high + " are: "); for (int i = low; i <= high; i++) { if (isArmstrong(i)) { System.out.println(i); } } } }
- The
Scanner
class is used for capturing user input. - The
for
loop runs through each number in the user-specified range. - The
isArmstrong()
method is called to check each number.
- The
Conclusion
By following the steps outlined above, you now have a Java program capable of identifying and displaying Armstrong numbers within a specified interval. Such tasks enhance understanding of functions, loops, and basic number manipulation in Java. Implementing these concepts in practical applications like finding Armstrong numbers provides valuable hands-on experience in programming basics. This knowledge applies across various scenarios, making your programming journey easier and more efficient.
No comments yet.