I hope you guys are enjoying the core Java or basic programming series. In this particular post, we are going to cover how to find out if the given number is on Armstrong number.
If you’re just starting to learn about programming, you might be wondering what an Armstrong number is. Also called the narcissist number, it’s a special case that behaves differently depending on the base of the number system. So it’s worth paying attention to if you’re learning a new programming language.
Post Type: | Java Programs For Beginners |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | Join Our Telegram Group |
What is a Armstrong Numbers?
A number is called an Armstrong number if the sum of its digits each raised to the power of the total number of digits is equal to that same number.
A great way to check if a number is an Armstrong number is to take that number, raise each digit of the number to the power of how many digits are in the original number, and then add all those numbers up. If your answer is equal to the original numeral, you have found an Armstrong one!
A simple number like 153 can be broken down into digits 3, 5, and 1. If we raise each digit to the power of 3 and then add them together, we get 153.
Armstrong Number Logic
One to understand the logic of Armstrong nos, that should satisfy all the properties of Armstrong nos. To understand an Armstrong nos let’s take the number 548834 and check if it satisfies the Armstrong nos property.
4 Digit Armstrong number in java
Similarly lets take an another number which is 122, Here the number has 3 digits so we have to do the cube of each number which is
1222= 14 + 24 + 24 + 24 = 1+16+16+16 = 49
So the each digits sum is not the original number. Thats why the 1222 is not an narcissist number.
6 Digit Armstrong number in java
There are 6 numbers and let us find out each digits power of 6 and after that sum of all digits sums
548834 = 56+46+86+86+36+46
(5*5*5*5*5*5)+(4*4*4*4*4*4)+(8*8*8*8*8*8)+(8*8*8*8*8*8)+(3*3*3*3*3*3)+(4*4*4*4*4*4)
15265+4096+262144+262144+729+4096 = 548834
After adding the sum of all digits we are getting the same numbers thats why 548834 is an ArmStrong Numbers.
Armstrong Numbers Examples
In this section we are sharing some of the Armstrong Number Examples which will help you all the readers to understand the topics very well.
Here are the some of the Armstrong Numbers Examples: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153, 4679307774, 32164049650, 32164049651.
Note: There is no Armstrong Number in two digits.
Armstrong Numbers Programs
Here we will try to Verify a number is an Armstrong Number or not.
Armstrong Numbers Example 1
Lets take a three digit number and check whether the number is a Armstrong Number or not.
package com.softwaretestingo.basic; import java.util.Scanner; public class ArmstrongNumberEx1 { public static void main(String[] args) { int number, originalNumber, remainder, result = 0; Scanner in=new Scanner(System.in); System.out.println("Enter the number"); number=in.nextInt(); originalNumber = number; while (originalNumber != 0) { remainder = originalNumber % 10; result = result + (remainder*remainder*remainder); originalNumber /= 10; } if(result == number) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); } }
Output:
Enter the number: 153 153 is an Armstrong number.
Enter the number: 122 122 is not an Armstrong number.
If you try the above pice of code with a 3 digit number but apart from 3 digit if you tried with other digit number then the above piece of code will be failed.
So let’s try to understand with another approach where it will pass with any number of digits. For that we will take the help of the math matical functions help. But before jump into that program we need to understand one more concept which is:
Java Program to Count Number of Digits in an Integer?
package com.softwaretestingo.basic; import java.util.Scanner; public class CountNoOfDigits { public static void main(String[] args) { int number,originalnumber, count=0; Scanner in=new Scanner(System.in); System.out.println("Enter the number: "); originalnumber=in.nextInt(); number=originalnumber; while (number != 0) { // number = number/10 number /= 10; ++count; } System.out.println("Total Number Of Digits In "+originalnumber+" is: "+count); } }
In the above code we have used the while loop but you can do this using the for loop also. Lets see how to do that:
package com.softwaretestingo.basic; import java.util.Scanner; public class CountNoOfDigits2 { public static void main(String[] args) { int number,originalnumber, count=0; Scanner in=new Scanner(System.in); System.out.println("Enter the number: "); originalnumber=in.nextInt(); number=originalnumber; for (; number != 0; number /= 10, ++count) { } System.out.println("Total Number Of Digits In "+originalnumber+" is: "+count); } }
The above for loop is valid or execute when the number is not Zero (0). On each iteration of For loop the number is divided by 10 and in the same time the count is also increased.
Now, let us go back to parent topic which is Armstrong Number. In the above programs we have learnt how to get the number of digits in a enter integer. With the help of the Count Number of Digits in an Integer program we can validate an entered number is an amstrong number or not.
4 Digit Armstrong number in java
Now we will learn how to write a Java program that can determine if a four-digit number is an Armstrong number. Here is an example of such code.
package com.softwaretestingo.basic; import java.util.Scanner; public class ArmstrongNumberEx2 { public static void main(String[] args) { int number, originalNumber, remainder, count=0, result = 0; Scanner in=new Scanner(System.in); System.out.println("Enter the number: "); number=in.nextInt(); originalNumber = number; // Findout the Number Of Digits in the Entered Integer for (;originalNumber != 0; originalNumber /= 10, ++count); originalNumber = number; for (;originalNumber != 0; originalNumber /= 10) { remainder = originalNumber % 10; result += Math.pow(remainder, count); } if(result == number) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); } }
Output:
Enter the number: 1741725 1741725 is an Armstrong number.
Enter the number: 5 5 is an Armstrong number.
The above code is suitable for n digits of integer. But we will another interview questions which some time we face in the interview.
Print the Armstrong number up to the specified limit?
In the below program we will print all the Armstrong number between 0 to the entered Numbers.
package com.softwaretestingo.basic; import java.util.Scanner; public class ArmstrongNumberEx3 { static boolean isArmstrong(int number) { int originalNumber, remainder=0, result=0, count=0; //assigning n into a temp variable originalNumber=number; // Findout the Number Of Digits in the Entered Integer for (;originalNumber != 0; originalNumber /= 10, ++count); originalNumber = number; for (;originalNumber != 0; originalNumber /= 10) { remainder = originalNumber % 10; result += Math.pow(remainder, count); } if(result == number) return true; else return false; } public static void main(String[] args) { int number; Scanner in=new Scanner(System.in); System.out.println("Enter the number: "); number=in.nextInt(); System.out.println("Armstrong Number up to "+ number + " are: "); for(int i=0; i<=number; i++) { //function calling if(isArmstrong(i)) //prints the armstrong numbers System.out.print(i+ ", "); } } }
Output:
Enter the number: 1000 Armstrong Number up to 1000 are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407,
Conclusion:
In this blog post we have discussed in details about Armstrong Numbers. and also we have discuss about how to find the number of digits in the entered integer. After reading this blog post if you face any problem let us know in the comment section.
Leave a Reply