
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Check Armstrong Number
In this article, we will understand how to check if the given number is an Armstrong number. An Armstrong number is a number that is equal to the sum of the cubes of its own digits.
An integer is called an Armstrong number of order n if it's every digit separate out and cubed and summed up then the sum will be same as the number i.e. abcd... = a3 + b3 + c3 + d3 + ...
In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example: 153 is an Armstrong number.
153 = 13 + 53 + 33
For example: 371 is an Armstrong number.
Below is a demonstration of the same −
Input
Suppose our input is −
Enter the number : 407
Output
The desired output would be −
407 is an Armstrong number
Algorithm
Step 1 - START Step 2 - Declare four integer values namely my_input, my_temp, my_remainder, my_result Step 3 - Read the required values from the user/ define the values Step 4 - Run a while loop to check Armstrong numbers using %, / and * operator Step 5 - Divide by 10 and get remainder for ‘check’ . Step 6 - Multiply ‘rem’ thrice, and add to ‘sum’, and make that the current ‘sum’. Step 7 - Divide ‘check’ by 10, and make that the current ‘check’. Store the resultant value. Step 8 - If the resultant value is equal to the input value, the input value is an Armstrong number, else it’s not an Armstrong number Step 9 - Display the result Step 10- Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .
import java.util.Scanner; public class IsArmstrong { public static void main(String[] args) { int my_input, my_temp, my_remainder, my_result; my_result = 0; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the number : "); my_input = my_scanner.nextInt(); my_temp = my_input; while (my_temp != 0){ my_remainder = my_temp % 10; my_result += Math.pow(my_remainder, 3); my_temp /= 10; } if(my_result == my_input) System.out.println(my_input + " is an Armstrong number"); else System.out.println(my_input + " is not an Armstrong number"); } }
Output
Required packages have been imported A reader object has been defined Enter the number : 407 407 is an Armstrong number
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class IsArmstrong { public static void main(String[] args) { int my_input, my_temp, my_remainder, my_result; my_input = 407; my_result = 0; System.out.println("The number is defined as " +my_input); my_temp = my_input; while (my_temp != 0){ my_remainder = my_temp % 10; my_result += Math.pow(my_remainder, 3); my_temp /= 10; } if(my_result == my_input) System.out.println(my_input + " is an Armstrong number"); else System.out.println(my_input + " is not an Armstrong number"); } }
Output
The number is defined as 407 407 an Armstrong number
- Related Articles
- Java Program to Check Armstrong Number between Two Integers
- C++ Program to Check Armstrong Number
- Python Program to Check Armstrong Number
- C Program to Check Armstrong Number?
- Swift Program to Check Armstrong Number
- Java program to check whether the given number is an Armstrong number
- Golang Program to Check For Armstrong Number
- Haskell program to check armstrong number between two integers
- Kotlin Program to Check Armstrong Number between Two Integers
- Java Program to Display Armstrong Number Between Two Intervals
- Write a C# program to check if the entered number is Armstrong number?
- C# Program to Check Whether the Entered Number is an Armstrong Number or Not
- Armstrong number in Java.
- How to Check Armstrong Number between Two Integers in Golang?
- Check the number is Armstrong or not using C
