Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
In number theory, a narcissistic number, an Armstrong number is named after Michael F. Armstrong is a number that is the sum of its own digits each raised to the power of the number of digits. In this Armstrong Number in Java article, let’s learn how to check whether a given number is Armstrong number or not.
The topics discussed in this article are:
The sum of the power of individual digits is equal to the number itself. Between 1 to 1000, there are five Armstrong numbers. They Are :- 1, 153, 370, 371, 407. Here’s the general equation.
abcd... = an + bn + cn + dn + ...
Let’s check out the concept with some examples.
Example1: 370
3*3*3 + 7*7*7 + 0*0*0 = 27 + 343 + 0 = 370
Example2: 407
4*4*4 + 0*0*0 + 7*7*7 = 64 + 0 + 343 = 407
I hope that you are clear with the concept now. Moving on, let check out how to check whether a given number is Armstrong number or not in Java.
You can check whether a given number is Armstrong number or not in Java in two ways:
In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. The example program below checks if a given 3 digit number is Armstrong number or not.
package MyPackage; public class ArmstrongNumber{ public static void main(String[] args) { int num = 371, originalNum, remainder, result = 0; originalNum = num; while (originalNum != 0) { remainder = originalNum % 10; result += Math.pow(remainder, 3); originalNum /= 10; } if(result == num) System.out.println(num + " is an Armstrong number."); else System.out.println(num + " is not an Armstrong number."); } }
Output: 371 is an Armstrong number.
The steps listed in the code are:
package MyPackage; public class Armstrong { public static void main(String[] args) { int number = 9474, originalNumber, remainder, result = 0, n = 0; originalNumber = number; for (;originalNumber != 0; originalNumber /= 10) { n++; } originalNumber = number; for (;originalNumber != 0; originalNumber /= 10) { remainder = originalNumber % 10; result += Math.pow(remainder, n); } if(result == number) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); } }
Output:
9474 is an Armstrong number.
Here, we have two for loops. The first one calculates the number of digits in the given number. The second loop checks if the given number is Armstrong number or not.
With this, we have reached towards the end of this article. I hope the content explained above added value to your Java knowledge. Keep reading, keep exploring!
Check out the Java Certification Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.
Got a question for us? Please mention it in the comments section of this “Armstrong number in Java” blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co