Armstrong Numbers In Java Example

I hope you guys are enjoying the core Java or basic programming series. In this post, we will cover how to find out if the given number is Armstrong’s 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 Interview Questions
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

What is an 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

To understand the logic of Armstrong nos, one must satisfy all its properties. 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, let’s take another number, 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, each digit sum is not the original number. That’s why 1222 is not a narcissistic number.

6 Digit Armstrong number in Java

There are 6 numbers, and let us find out each digit’s power of 6, and after that, the 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 get the same numbers; that’s why 548834 is an Armstrong Number.

Armstrong Numbers Examples

In this section, we are sharing some of the Armstrong Number Examples, which will help 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

Let’s take a three-digit number and check whether the number is an 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 piece of code with a 3-digit number, but if you try it with a number other than 3 digits, then the above piece of code will fail.

So let’s try to understand with another approach where it will pass with any number of digits. For that, we will use the help of the magic functions in math. But before jumping 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. Let’s 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 executes when the number is not Zero (0). On each iteration of the for loop, the number is divided by 10, and at the same time, the count is also increased.

Now, let us go back to the parent topic, which is the Armstrong Number. In the above programs, we have learned how to get the number of digits in an integer. With the help of the Count Number of Digits in an Integer program, we can validate whether an entered number is an Armstrong 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 integers. However, we will have other interview questions, which we sometimes face during the interview.

In the below program, we will print all the Armstrong numbers between 0 and 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 Armstrong Numbers in detail and also how to find the number of digits in the entered integer. After reading this blog post, if you face any problems, let us know in the comment section.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment