Write a java program to check if a number is Armstrong number or not?

import java.util.Scanner;

class ArmstrongNumber {

	int number;
	//Scanner scanner;

	void getValue() {

		scanner = new Scanner(System.in);
		
		System.out.println("Enter a  number:");
		
		number = Integer.parseInt(scanner.nextLine());
	}

	void checkArmstrong() {

		int temp = number;
		int sum = 0;
		int mod = 0;

		while (temp != 0) {

			mod = temp % 10;
			sum = sum + (mod * mod * mod);
			temp = temp / 10;
		}

		if (sum == number)
			System.out.println(number + " is an Armstrong Number");
		else
			System.out.println(number + " is not an Armstrong Number");
	}
}

public class Main {

	public static void main(String args[]) {

		ArmstrongNumber armstrongNumber = new ArmstrongNumber();

		armstrongNumber.getValue();
		armstrongNumber.checkArmstrong();
	}
}
Output
Enter a number: 401 401 is not an Armstrong Number Enter a number: 407 407 is an Armstrong Number



















Receive Latest Java Examples in Your Email: