Armstrong Number In Java Program

So now in this post you will learn Armstrong number in java program.

Armstrong Number

Basically an Armstrong number is sum of cube of its digits. Let me explain you with an example,

Armstrong Number Example

371 is an Armstrong number. How?

Here it is,

371 = (3 * 3 * 3) + (7 * 7 * 7) + (1 * 1 * 1)

Now cubes of above digits is,

(3 * 3 * 3) = 27

(7 * 7 * 7) = 343

(1 * 1 * 1) = 1

So, 27 + 343 + 1 = 371

Hence, 371 is an Armstrong number where sum of the cubes of its digits is equal to number itself.

example: ARMSTRONG NUMBER IN JAVA PROGRAM

Armstrong Number In Java Program

Also Read – Java Garbage Collection

import java.util.Scanner;

public class Armstrong_Number {

public static void main(String[] args) {

int x;
int y;
int z = 0;
int temp;

Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number to find if it is an Armstrong number: ");
x = sc.nextInt();

temp = x;
while(x > 0){
y = x % 10;
x = x / 10;
z = z + (y * y * y);
}

if(temp == z){
System.out.println(temp + " is an Armstrong Number.");
}
else{
System.out.println(temp + " is not an Armstrong Number.");
}
}
}

Output:

Please enter a number to find if it is an Armstrong number: 153
153 is an Armstrong Number.

About the author

admin

View all posts

Leave a Reply

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