Program for Armstrong Number in Java

Armstrong number is a digit number with the end goal that the whole of digits raised to the power n is equivalent to the number.

For instance:

153 is Armstrong number on the grounds that here n=3 and 13+53+33=153.

120 isn’t Armstrong number on the grounds that 13+23+03!=120.

Underneath I have shared a Java program that checks whether a number is Armstrong number or not.

Program for Armstrong Number in Java

import java.util.Scanner; //import Scanner class for reading from keyboard
 
class ArmstrongNumberExample
{
	public static void main(String...s)
	{
		Scanner sc=new Scanner(System.in);
		int num,len,sum=0,temp,rem;
		
		System.out.println("Enter a number:");
		num=sc.nextInt();
		temp=num;
	
		len=String.valueOf(num).length();
		
		while(temp!=0)
		{
			rem=temp%10;
			sum+=(int)Math.pow(rem,len);
			temp/=10;
		}
 
		if(sum==num)
			System.out.println("Armstrong Number");
		else
			System.out.println("Not Armstrong Number");
	}
}

Output

Program for Armstrong Number in Java

Leave a Comment

error: Alert: Content is protected!!