Armstrong number in Java: In Java, Armstrong numbers are almost the same as in the mathematical field. The Armstrong number corresponds to the number whose sum of digits raised to the number of digits is equal to the number itself. Another name for these armstrong numbers is narcissistic numbers. We will know more about the Armstrong number in Java and its significance in programming.
Armstrong Number in Java: Definition
In Java, a number is referred to as an Armstrong number if the total of its digits, increased by the power of the number of digits, equals the number itself. They are known by many names, such as Narcissistic and pluperfect numbers. For example, 371 is an Armstrong number, as 33+73+13 is equal to 371 itself.
Also Read: How to Get Input From Users in Java?
Armstrong Number in Java: How Do They Work?
In Java, the numbers are first split into their component digits. The separated digits are then multiplied by the number of digits in the predetermined number. After multiplying all these digits, the results are added together. It will be an Armstrong number if the total sum of all the digits is the same as the number itself. Let us make you understand step by step.
- First, we must determine the number of digits in the number given.Â
- We divide the number into its digits by using the while loop and modulus functions.
- The digits are then multiplied by the predetermined number of digits.
- After multiplication, all these digits are added and stored in a variable.
- Now check if the result obtained after the sum is equal to the original number.
- It is an Armstrong number if the outcome is the same as the initial original number.
Also Read: What Is the Purpose of AJAX in JavaScript
Armstrong Number in Java: Function to check Armstrong NumberÂ
Let us write a function in Java to check if a given number is an Armstrong number.
Armstrong Number in Java |
import java.util.Scanner;
public class ArmstrongNumber {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         System.out.print(“Enter a number: “);         int number = scanner.nextInt();         if (isArmstrong(number)) {             System.out.println(number + ” is an Armstrong number.”);         }        else {             System.out.println(number + ” is not an Armstrong number.”);         }     }     // Function to whether a number is an Armstrong number     static boolean isArmstrong(int num) {         int originalNum, rem, result = 0, n = 0;         originalNum = num;         // Count the number of digits         while (originalNum != 0) {             originalNum /= 10;             ++n;         }         originalNum = num;         // Determine the sum of the nth power of each digit.         while (originalNum != 0) {             rem = originalNum % 10;             res += Math.pow(rem, n);             originalNum /= 10;         }         // Verify that the outcome is the same as the initial original number.         return res == num;     } } |
Also Read Technical Topics
- Java Tutorial
- What is Data Science?
- What is C?
- What is Data Analysis?
- Web Development
- Python Tutorial
Armstrong Number in Java: Decode is Armstrong Function
Now let us understand the is Armstrong function in Java with the following steps.
Armstrong Number in Java: is Armstrong Function |
static boolean isArmstrong (int num) {
        int originalNum, remainder, res = 0, n = 0;         originalNum = num;         // Count the number of digits         while (originalNum != 0) {             originalNum /= 10;             ++n;         }         originalNum = num;         // Determine the sum of the nth power of every digit         while (originalNum != 0) {             remainder = originalNum % 10;             res += Math.pow(remainder, n);             originalNum /= 10;         }         // Verify that the outcome is the same as the original number.         return res == num;     } |
- Initially, put the initial figure in a variable.Â
- Now count the number of digits using the while loop.
- Now, use the modulus operator to use these digits and multiply it by the number of digits in the number.
- Add all these digits and then store them in a variable.
- Compare the original number with the result obtained. If the number obtained is the same as the original number.
Recommended Technical Course
- Full Stack Development Course
- Generative AI Course
- DSA C++ Course
- Data Analytics Course
- Python DSA Course
- DSA Java Course
Learn Java + DSA with PW SkillsÂ
Join our Decode Java with DSA Course with PW Skills and harness the strength of DSA to effectively resolve complicated issues. Gain a strong foundation in Java and Data Structure to get job roles such as Java Developer, Software Engineer, Software Developer, Backend Developer, etc.
Master some of the in-demand technologies such as Eclipse, notepad++, Leetcode, Linkedin, Java, Github, DSA, etc. The course will offer core real-time projects, practice exercises, doubt-clearing sessions, PW Lab, Recorded videos, quizzes, placement assistance, etc. Know more at @pwskills.com
For Latest Tech Related Information, Join Our Official Free Telegram Group : PW Skills Telegram Group
Armstrong Number in Java FAQs
What is the importance of the Armstrong Number?
A number is said to be an Armstrong number in Java when the sum of its digits, each raised to the power of the number of digits, is equal to the number itself, which is said to be an Armstrong number.
Can Armstrong's number be negative?
No, Armstrong numbers can’t be negative. They cannot be negative or fractional. The sum of the product of a number of digits of a non-zero digit number can never be negative.
Is 153 an Armstrong number?
Yes, the sum of digits with the products of a number is equal to the number 153. Hence, it is an Armstrong number. We can easily predict whether a number is an Armstrong number or not using the is Armstrong function logic easily.