Armstrong Number is a number that is equal to the sum of cubes of its digits.
Example - 0,1,153,370,371 etc.
For example, if you have 153, it is an Armstrong number because 13+53+33 = 153;
Let's see the java program for determining if a number is Armstrong Number or not.
import java.util.Scanner;
class Armstrong
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int sum = 0,temp = n, arm = 0;
while(temp!=0)
{
arm =temp%10;
sum+= Math.pow(arm,3);
//It can also be written as sum+= (arm*arm*arm);
temp/=10;
}
if(sum == n)
{
System.out.println("It is an Armstrong number");
}
else
{
System.out.println("It is not an Armstrong number");
}
}
}
Demonstration:
2. A class namely Armstrong have been defined.
3. A main method have been defined.
4. Object of Scanner class have been created.
5. A Number has been demanded from user by giving a message.
6. To accept and store the number, a variable namely n has been defined of int data type
7. Then, three different variables namely
a) sum - This variable will store the cubes of each digit in number. First, it is initialized with 0.
b) temp - This variable will hold the number given by user on temporary basis. Actually, entire operation will be done with temp variable. temp = n means the value stored in n namely variable is getting assigned to temp. It means that throughout the entire operation, original value stored in n namely variable will remain safe.
c) arm - This variable will hold the individual digit in a number given by user.
Now, let us see the working:
while(temp!=0)
This loop will continue its iteration until value of temp doesn't become equals to 0. Say for example, user gave 153. Here, first 3 will be extracted, then 5 and at last, 1 will get extracted as it is in index 0. Refer to the below diagram for clear understanding.
arm =temp%10;
sum+= Math.pow(arm,3);
Here, we need to understand few things.
First, individual digits from given number is getting extracted using temp%10 and getting stored in arm.
As in case of 153, the first extracted number using temp%10 will be 3.
Then, with the pow method of the Math class, cube of the number will be determined and it will be added to the variable sum.
Similarly, second number using temp%10 will be 5 and its will cube will also determined by pow method of Math class and then, it will be added to sum.
At last, third number using temp%10 will be 1 and its will cube will also determined by pow method of Math class and then, it will be added to sum.
temp/=10;
In previous operation, we have seen that the first extracted number using temp%10 was 3. Immediately, after this operation, temp/=10; will be executed which means 153 is now going to be divided by 10 resulting to 15.3. This operation will permanently delete this 3 from temp variable. so that in next iteration, 3 doesn't gets added to the sum variable again. Like this, it will keep on deleting the digits from temp variable.
Here is the final outcome:
sum is now containing cube of digits i.e. 1+125+27 = 153
n variable is containing 153
Finally, with the conditional statement using if-else, it is determined if sum is equal to n or not as it can be seen in the program. If sum is equal to n, then it is displaying that the number is a Armstrong number. Otherwise, it is displaying that the number is not an Armstrong number.
Now, write this program and test it with various numbers and increase your knowledge.
Happy Coding.......