Java program to check the given number is Armstrong or not


What is Armstrong number?

            An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits. Refer more information on Wiki

How to find it?

    153 = (1*1*1) + (25*25*25) + (3*3*3)

For a three digit number sum of cube of each number should be equal to input number  

        Find more information about Armstrong number


Find all Armstrong number between 1 to 1000

Simple Java program to find Armstrong number

        Run this below example with different input.

public class ArmstrongNumber
{
	public static void main(String args[])
	{
		int n, arg, sum = 0, r;

		n = 153; // input value
		arg = n;
		for (int i = 1; i < n; i++)
		{
			while (n > 0)
			{
				r = n % 10;
				sum = sum + (r * r * r);
				n = n / 10;

			}

		}
		if (arg == sum)
		{
			System.out.println("Given number is armstrong number: " + arg);
		}
		else
		{
			System.out.println("Given number is not armstrong number: " + arg);
		}
	}

}

Output

Given number is armstrong number: 153




Related Post

Comments


©candidjava.com