Did you know that the volume of a pizza of radius ‘z’ and height ‘a’ is equal to pi*z*z*a! Mathematics and its exciting and mysterious world is something that intrigues everyone.
Armstrong numbers are positive n-digit numbers that are equal to the sum of their digits’ nth powers. An Armstrong number of order n (order being the number of digits) is a positive integer with n digits following the below condition.
abcd.. = an + bn + cn + dn + …
For example, 153 = 13 + 53 + 33 = 1 + 125 + 27 = 153
Thus, 153 is an Armstrong number.
What is Armstrong Number in Java
A special kind of number called an Armstrong number in Java is one whose total digits, each raised to the power of the number of digits in the number, equals the original number itself.
For example, 407 = 43 + 03 + 73= 64 + 0 + 343 = 407
Thus, 407 is an Armstrong number.
In other words, if the sum of an n-digit number's digits, each raised to the nth power, equals the number itself, then the number is said to be an Armstrong number. This idea is frequently used as a programming exercise to show Java programmers how to use loops and mathematical calculations.
In this article, we will use these properties of Armstrong numbers to find whether a number is an Armstrong number or not. Let’s see how!
We are given a number, n. Our aim is to find whether n is an Armstrong Number or not.
Now, we can follow various approaches to solve this problem but first, let us see the algorithm to find whether the number is an Armstrong number.
Algorithm
To solve this problem, we may follow the following algorithm:
Start
Read the number to be checked.
Start a loop to count the number of digits in the number.
Start another loop to calculate the sum of each digit raised to the power of the total number of digits.
Check if the sum is equal to the number
If true, the number is an Armstrong number.
If false, the number is not an Armstrong number.
End
Approach 1: Using For loop
In this approach, we are using for loop. In this approach, we create a boolean function known as Armstrong, which returns true or false.
Inside the function, there are 2 for loops. The first loop calculates the total number of digits in the given number. The second loop calculates the sum of each digit when raised to the power of the total number of digits in the given number. We use an if-else statement that checks if the sum is equal to the number. If the condition is true, it returns true; otherwise, it returns false.
The code and output of this approach are following.
Program 1
import java.util.Scanner;
import java.lang.Math;
public class ArmstrongNumber
{
// function to check if the number is an Armstrong or not
static boolean isArmstrong(int num)
{
int temp, digits=0, last=0, sum=0;
temp = num;
for(; temp > 0; temp = temp/10)
{
digits++;
}
temp = num;
for(; temp > 0; temp = temp/10)
{
last = temp % 10;
sum += (Math.pow(last, digits));
}
if(num == sum)
return true;
else
return false;
}
//driver code
public static void main(String args[])
{
int n;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number to be checked: ");
n = sc.nextInt();
if ( isArmstrong(n) )
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
}
You can also try this code with Online Java Compiler
In this approach, we are using a while loop. We use a boolean function containing two while loops and an if-else statement in this approach. This function is called Armstrong.
The first loop in the function calculates the number of digits in the given number. The second loop works to calculate the sum of each digit when raised to the power of the total number of digits. The if-else statement is used to return true or false depending on whether the condition sum is equal to the number is satisfied or not.
This is almost similar to the approach we followed for For loop. The code and output of this approach are following.
Program
import java.util.Scanner;
import java.lang.Math;
public class ArmstrongNumber
{
// function to check if the number is an Armstrong or not
static boolean isArmstrong(int num)
{
int temp, digits=0, last=0, sum=0;
temp = num;
while(temp > 0)
{
temp = temp/10;
digits++;
}
temp = num;
while(temp > 0)
{
last = temp % 10;
sum += (Math.pow(last, digits));
temp = temp/10;
}
if(num == sum)
return true;
else
return false;
}
//driver code
public static void main(String args[])
{
int n;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number to be checked: ");
n = sc.nextInt();
if ( isArmstrong(n) )
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
}
You can also try this code with Online Java Compiler
In this approach, instead of using the pow function, we create our own function to calculate the exponential value.
In the power function, we use Recursion to calculate the exponent value. In each iteration, we are making the recursive call twice, i.e. power(x,y/2). Now, depending upon whether the power is even or odd, we multiply an x with it or not. The recursive calls are made till the value of y becomes zero and the function returns 1.
The code and output of this approach are following.
Program
import java.io.*;
import java.util.Scanner;
public class Armstrong
{
// Function to calculate x raised to the power y
int power(int x, long y)
{
if( y == 0)
return 1;
if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
return x*power(x, y/2)*power(x, y/2);
}
// Function to calculate number of digits
int digit(int x)
{
int n = 0;
while (x != 0)
{
n++;
x = x/10;
}
return n;
}
// Function to check if the number is Armstrong or not
boolean isArmstrong (int num)
{
int n = digit(num);
int temp=num, sum=0;
while (temp!=0)
{
int last = temp%10;
sum = sum + power(last,n);
temp = temp/10;
}
if (sum == num)
return true;
else
return false;
}
//driver code
public static void main(String args[])
{
Armstrong ob = new Armstrong();
int n;
System.out.print("Enter the number to be checked: ");
Scanner in= new Scanner(System.in);
n = in.nextInt();
if ( ob.isArmstrong(n) )
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
}
You can also try this code with Online Java Compiler
Iterate through numbers from 1000 to 9999, add their digit sums raised to the fourth power, then verify that the result is equal to the original number to obtain 4-digit Armstrong numbers in Java.
Q. What are the first 10 Armstrong numbers?
The first ten Armstrong numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The special characteristic of these numbers is that the total of their digits, each increased to the power of the digits, equals the original number.
Q. What is the Armstrong number in Java 1634?
In Java, the number 1634 is an Armstrong number. It satisfies the requirement since 1^4 + 6^4 + 3^4 + 4^4 = 1634, where the sum of each digit raised to the fourth power matches the number itself.
Conclusion
This article taught us how to find whether a number is an Armstrong number or not in Java. We saw how to solve this problem using for loop and while loop. We also tried to solve the problem without using built-in functions like pow.
We hope this blog has helped you enhance your knowledge. If you would like to learn more, check out our articles on Introduction to Java and Decision Statements in Java. If you want to learn more about the basics of Java, follow this guided path Basics Of Java. Do upvote our blog to help other ninjas grow.