java
Parameters: int startRange, int endRange
startRange and endRange define the range in which to find Armstrong numbers
Returns: ArrayList of Armstrong numbers in given range
This function employs nested loops and conditionals in Java to find all Armstrong numbers within a provided range.
Hello programmers, welcome to this crisp blog post. Today, we'll delve into how to program the find-armstrong-numbers-range function using Java. An Armstrong number, for those who don't know, is one that equals the sum of its digits when raised to the power of the number of digits. Sounds intriguing, right? Stay tuned as we step through each line of code, explaining it's part in solving this interesting mathematical problem. Let's learn and grow together!
To solve this problem, we need to have a clear understanding of Armstrong numbers. An Armstrong number of a certain number of digits is an integer for which the sum of its own digits each raised to the power of the number of digits is equal to the number itself. For example, '153' is an Armstrong number as 111 + 555 + 333 is equal to 153. Now we want to write a function in Java that finds all the Armstrong numbers in a given range. No input can be provided that will contain no Armstrong numbers. Also inputs provided to the function will be always in ascending order.
Let's start by creating a function named findArmstrongNumbersInRange
. This function takes in two parameters, start
and end
, which represents the range of numbers.
public static void findArmstrongNumbersInRange(int start, int end) {
}
Iterate through the given range starting from start
to end
. Inside the loop body, you will then implement the logic to check for Armstrong numbers.
public static void findArmstrongNumbersInRange(int start, int end) {
for(int i = start; i <= end; i++) {
// Logic goes here
}
}
To check whether a number is an Armstrong number or not, we can create a separate method named isArmstrongNumber
. It will sum the cubes of its digits and compare the sum to the number itself. If they are equal, our method will return true; otherwise, it will return false.
private static boolean isArmstrongNumber(int number) {
int temp = number;
int sum = 0;
while (temp != 0) {
int remainder = temp % 10;
sum = sum + (remainder * remainder * remainder);
temp = temp / 10;
}
return number == sum;
}
We then call this method in our loop, if true we print out the number.
public static void findArmstrongNumbersInRange(int start, int end) {
for(int i = start; i <= end; i++) {
if(isArmstrongNumber(i)) {
System.out.println(i);
}
}
}
We have successfully implemented a function that can find all Armstrong numbers within a given range. The final code is listed below.
public class Main {
public static void main(String[] args) {
findArmstrongNumbersInRange(1, 999);
}
public static void findArmstrongNumbersInRange(int start, int end) {
for(int i = start; i <= end; i++) {
if(isArmstrongNumber(i)) {
System.out.println(i);
}
}
}
private static boolean isArmstrongNumber(int number) {
int temp = number;
int sum = 0;
while (temp != 0) {
int remainder = temp % 10;
sum = sum + (remainder * remainder * remainder);
temp = temp / 10;
}
return number == sum;
}
}
I hope this guide has given you a clear understanding of how to find Armstrong numbers in a range in Java. Happy coding!
An Armstrong number is a number that is equal to the sum of its digits, each raised to the power of the number of digits. For example, 371 is an Armstrong number because `3^3 + 7^3 + 1^3 = 371`. This function uses this principle to check and return all Armstrong numbers in a certain range.
Learn more