Armstrong Number Program in Java

In this tutorial we will learn writing Armstrong number program in Java to check a given input number is Armstrong or not?

So before moving directly on the writing program Lets know about Armstrong number:

Click Here:  What is Armstrong Number? Armstrong program in C.

Program 1: Armstrong Program in Java

This is a traditional approach using a while loop to check given number is Armstrong number or not. Here using while loop we calculate the number of digits. Then iterated through each digit, raising it to the power of the calculate total digits.

After the sum calculation, checked if the sum matched the original number. If matched then it is an Armstrong number otherwise it is not an Armstrong number.

import java.util.*;  
import java.lang.Math;
class Main{
    public static void main(String ...a){
    int i=0,n,result=0,number1,temp;
    Scanner sc= new Scanner(System.in);
    System.out.print("Enter number- ");  
    int number= sc.nextInt();  
    number1=number;
	temp=number;
	while(number!=0){
		number=number/10;
		i++;
	}
	while(number1!=0){
		n=number1%10;
		result=result+(int)Math.pow(n,i);
		number1=number1/10;
		}
	if(temp==result)
		System.out.println("Armstrong Number");
	else
		System.out.println("Not an Armstrong Number");
    }
}

Output:

Enter number- 153
Armstrong Number

Program 2: Armstrong Program using Java 8

Here we are taking the Advantage of Java 8 streams. We are using IntStream to iterate through the digits and then calculating the power using Math.pow() and finding the sum using sum() method.

import java.util.stream.IntStream;
public class ArmstrongProgram {
    public static void main(String ...args){
        int input = 153;
        int output = IntStream.iterate(input,n->n>0,n->n/10)
                .map(digit -> (int)Math.pow(digit%10,String.valueOf(input).length()))
                .sum();
        System.out.println(output);
        if(output==input)
            System.out.println("armstrong number");
        else
            System.out.println("not armstrong number");
    }
}

Output

Armstrong Number

Conclusion

In this tutorial, we have learnt the concept of Armstrong numbers and implemented two Java programs to determine whether a given number is an Armstrong number. We have seen using Traditional logic and using java 8.

Understanding Armstrong Numbers:

An Armstrong number (also known as a narcissistic, pluperfect, or pluperfect digital invariant number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

Program 1: Traditional Approach

  • We took a traditional approach using a while loop to calculate the number of digits and then iterated through each digit, raising it to the power of the total digits.
  • The program checked if the sum matched the original number, determining whether it is an Armstrong number.

Program 2: Java 8 Stream Approach

  • Leveraging the power of Java 8 streams, the second program utilized IntStream to iterate through the digits and calculate the sum using a more concise approach.
  • The result was compared with the original number to determine whether it is an Armstrong number.

Happy coding! 🚀

Leave a Comment