Armstrong Number In Java

1Armstrong Number in Java

The Armstrong number algorithm is a common question in academic settings and Java coding interviews.

2Armstrong Number

A positive m-digit number that is the sum of its digits’ mth powers is known as an Armstrong number. Additional names for it include pluperfect, Plus Perfect, and Narcissistic number. The sequence is A005188 from the OEIS. Let’s provide an example to help you grasp it.Join the Java course in Chennai to become a Java specialist.

1: 11 = 1

2: 21 = 2Play Video

3: 31 = 3

125: 13 + 23 + 53 = 1 + 8 + 125 = 134 (Not an Armstrong Number)

153: 13 + 53 + 33 = 1 + 125+ 27 = 153

1634: 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1643

Between 0 and 999, the first few Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, and 407. 

Additional Armstrong numbers include 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153, 4679307774, and 32164049651.

2.1Keep in mind there’s no Armstrong number with two digits.

You should be knowledgeable about the following Java programming concepts in order to comprehend this example:

  • Java while and do…while Loop
  • Java if…else Statement
  • Java for Loop

An Armstrong number of order n is a positive integer if and only if

An + Bn + Cn + Dn +… = ABCD

When an Armstrong number has three digits, the sum of each digit’s cubes equals the number itself. For instance:

153 is an Armstrong number and has the formula 1*1*1 + 5*5*5 + 3*3*3 =153//

Enroll in the JAva training in Chennai to understand the armstrong number and many other concepts in Java.

2.2Verifing the Armstrong number for the 3 digit number

public class Armstrong

{

public static void main(String[] args)

{

int number = 371, originalNumber, remainder, result = 0;

originalNumber = number;

while (originalNumber != 0)

{

remainder = originalNumber % 10;

result += Math.pow(remainder, 3);

originalNumber /= 10;

}

if(result == number)

System.out.println(number + ” is an Armstrong number.”);

else

System.out.println(number + ” is not an Armstrong number.”);

}

}

Output

371 is an Armstrong number

First, the value of the supplied integer (number) is stored in the originalNumber integer variable. This is so that we may compare the values of the final number and the starting number.

The originalNumber is then looped through using a while loop until it equals 0.

The final digit of num is saved in remaining at the end of each iteration.

he remainder is then multiplied by 3 using the Math.pow() function and added to the output.

After dividing by 10, the final digit of originalNumber is then eliminated.

In the end, the output and the number are compared. It is an Armstrong number if the values are equal. Otherwise, it isn’t.

Join the best java training institute in Chennai for impactful java training

2.3Verify the Armstrong number for the n digits

public class Armstrong {

 public static void main(String[] args) {

 int number = 8208, 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

8208 is an Armstrong number 

Rather than utilizing a while loop in our program, we’ve utilized two for loops.

The number’s digits are counted using the first for loop. It is the shortened version of:

 for (;originalNumber != 0; originalNumber /= 10) {

  n++;

}

The second for loop then does the calculation, powering the residual by the number of digits n for each repetition.

Learn more about armstrong number program and other frequently asked interview questions through enrolling in Java training in Chennai.