Find Armstrong Numbers using Java

5 simple ways to identify and print the Armstrong numbers using Java

The sum of the cubes of its digits is equal to the Armstrong number. Armstrong numbers include 0, 1, 153, 370, 371 and 407, for example.

The sum of an Armstrong number’s digits raised to the nth power equals the number itself.

Let’s look at why the number 154 is not an Armstrong number.

154 = (111)+(555)+(444)
Here:
(111)=1
(555)=125
(444)=64
Hence:
1+125+64=190, So 154 is not an Armstrong Number

Now, Let’s look at why the number 407 is an Armstrong number.

407 = (444)+(000)+(777)
Here:
(444)=64
(000)=0
(777)=343
Hence:
64+0+343=407 , So 407 is an Armstrong Number

In this example, you will learn how to use multiple Java methods to locate, identify, and print Armstrong numbers. Let’s take a look at each one individually.

1. Find Armstrong Number using While Type 1 :

public static void main(String[] args) {
	System.out.println("Find Armstrong Number using While Method 1 >> "+findArmstrongNumnerUsingWhileType1(1634));
}
public static boolean findArmstrongNumnerUsingWhileType1(int inputElement) {
	int finalResult=0;
	int temporyValue=inputElement;
	while (temporyValue != 0){
		finalResult += Math.pow(temporyValue % 10, Integer.toString(inputElement).length());
        temporyValue /= 10;
     }
	return finalResult == inputElement;
}

Output :

Find Armstrong Number using While Method 1 >> true

2. Find Armstrong Number using While Type 2 :

public static void main(String[] args) {
	System.out.println("Find Armstrong Number using While Method 2 >> "+findArmstrongNumnerUsingWhileType2("1634"));
}
public static boolean findArmstrongNumnerUsingWhileType2(String inputElement) {
	int finalResult=0;
	int temporyValue=inputElement.length();
	while (temporyValue > 0){
		finalResult += Math.pow(Integer.parseInt(inputElement.substring(temporyValue-1, temporyValue)),inputElement.length());
        temporyValue--;
     }
	return finalResult == Integer.parseInt(inputElement);
}

Result :

Find Armstrong Number using While Method 2 : >> true

3. Find Armstrong Number using FOR Loop :

public static void main(String[] args) {
	System.out.println("Find Armstrong Number using FOR >> "+findArmstrongNumnerUsingFor(1634));

}
public static String findArmstrongNumnerUsingFor(int element) {
	int  originalNumber, remainder, result = 0, n = 0;
    originalNumber = element;
    for (;originalNumber != 0; originalNumber /= 10, ++n);
    originalNumber = element;
    for (;originalNumber != 0; originalNumber /= 10)
    {
        remainder = originalNumber % 10;
        result += Math.pow(remainder, n);
    }
    if(result == element)
       return(element + " is an Armstrong number.");
    else
        return(element + " is not an Armstrong number.");
}


Result :

Find Armstrong Number using FOR >> 1634 is an Armstrong number.

4. Print all the Armstrong Numbers from the Range :

public static void main(String[] args) {
	System.out.println("Print Armstrong Numbers >> "+printArmstrongNumbers(1,10000));
}

public static List printArmstrongNumbers(int fromElement, int toElement) {
	List<Integer> armstrongNumbers = new LinkedList<>();
	for(int i = fromElement;i<toElement;i++) {
		int finalResult=0;
		int temporyValue=Integer.toString(i).length();
		while (temporyValue > 0){
			finalResult += Math.pow(Integer.parseInt(Integer.toString(i).substring(temporyValue-1, temporyValue)),Integer.toString(i).length());
	        temporyValue--;
	     }
		if(finalResult == i) armstrongNumbers.add(finalResult);
	}
	return armstrongNumbers;
}

Result :

Print Armstrong Numbers  >> [1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474]

5. Print Armstrong Number using Java Streams :

public static void main(String[] args) {
    System.out.println("Print Armstrong Number using Streams >> ");
	printArmstrongUsingStreams(1,10000);
}

public static void printArmstrongUsingStreams(int startElement ,int endElement) {
    IntStream.range(startElement, endElement)
            .filter((n) -> {
                final String number = Integer.toString(n);
                return number.chars()
                        .map(d -> d - '0')
                        .mapToDouble(v -> Math.pow(v, number.length()))
                        .sum() == n;
            }).forEach((x)->{
            	System.out.print(x+" ");
            });
}

Result :

Print Armstrong Number using Streams >> 
1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *