Java program to check the given number is Armstrong or not


Program

package com.candidjava;

import java.util.Scanner;

public class ArmstrongNumber
{

	public static void main(String args[])
	{
		int n, arg, sum = 0, r;
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the number to check armstrong number or not");
		n = in.nextInt();
		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

Enter the number to check armstrong number or not

153

Given number is armstrong number: 153

Enter the number to check armstrong number or not

121

Given number is not armstrong number: 121



Related Post

Comments


©candidjava.com