What Are Armstrong Numbers?
Armstrong numbers are special numbers where the sum of their individual digits, each raised to the power of the number of digits, equals the original number itself. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
The Java Code:
Let's break down the Java code provided:
package in.studyecart.impprograms; import java.util.Scanner; public class SECJAP112 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please Enter a Number :"); int actual_num = input.nextInt(), sum_num = 0, current_num = actual_num, count = 0; // Finding the count of digits in the given number while (actual_num != 0) { actual_num = actual_num / 10; count++; } actual_num = current_num; while (actual_num != 0) { int digit = actual_num % 10; sum_num = (int) (sum_num + Math.pow(digit, count)); actual_num = actual_num / 10; } if (current_num == sum_num) { System.out.println(current_num + " is Armstrong!!"); } else { System.out.println(current_num + " is Not Armstrong!!"); } } } (code-box)
Explaining the Code:
1. We start by taking input from the user, which is the number we want to check for Armstrong property.
2. We then find the count of digits in the given number using a while loop. This count will help us later in the calculation.
3. Next, we reset `actual_num` to the original number and enter another loop to calculate the sum of each digit raised to the power of the count of digits. This is where the magic happens in identifying Armstrong numbers.
4. Finally, we compare `current_num` (the original number) with `sum_num` (the calculated sum). If they are equal, we print that the number is Armstrong; otherwise, we print that it's not.
Armstrong numbers are a fun concept in mathematics and programming. This Java program allows you to determine whether a given number fits the Armstrong criteria. Try it out with different numbers and explore this fascinating mathematical property in action!