Check Armstrong Number Java Example

In this tutorial, We will show how to write a Program to Check Armstrong Number Java Example. The program in tested and output is posted below the screen.

ArmstrongNumber.java

package com.dineshkrish;
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to find that is armstrong or not: ");
int number = scanner.nextInt(); // Reading the Input
int a = number, r = 0, s = 0;
while (a != 0) {
r = a % 10;
a = a / 10;
s = s + r * r * r;
}
if (s == number) {
System.out.println("The given Number " + number + " is an armstrong number.");
} else {
System.out.println("Then given Number " + number + " is not an armstrong number.");
}
}
}

Output

Enter a number to find that is armstrong or not:
371
The given Number 371 is an armstrong number.

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *