Write a java program to find the given number is armstrong number or not ?

This java program checks if a number is armstrong or not.


								
import java.util.Scanner;

/**
 *
 * @author Mindclues
 */
class ArmstrongNumber {

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

        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number to check if it is an armstrong number");
        n = in.nextInt();

        temp = n;

        while (temp != 0) {
            r = temp % 10;
            sum = sum + r * r * r;
            temp = temp / 10;
        }

        if (n == sum) {
            System.out.println("Entered number is an armstrong number.");
        } else {
            System.out.println("Entered number is not an armstrong number.");
        }
    }
}

							

								
Enter a number to check if it is an armstrong number
121
Entered number is not an armstrong number.

Enter a number to check if it is an armstrong number
407
Entered number is an armstrong number.
							

Related Articles

post a comment