To Check Armstrong Number in Java
Basic Java Program
An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number.
- It has 3 digits.
- When you raise each digit to the power of 3 and add them up:
1³ + 5³ + 3³ = 153
Armstrong Number Calculation for 153:
- Count the digits: 153 has 3 digits.
- Calculate each digit raised to the power of 3:
- The first digit is 1: 1³ = 1
- The second digit is 5: 5³ = 125
- The third digit is 3: 3³ = 27
Add these values:
- Sum = 1 + 125 + 27 = 153
Since the sum (153) is equal to the original number (153), it confirms that 153 is an Armstrong number.
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter any number: ");
int number = scanner.nextInt();
int originalNumber = number;
int sumOfCubes = 0;
// Calculate the sum of cubes of digits
while (number > 0) {
int digit = number % 10;
number /= 10;
sumOfCubes += digit * digit * digit;
}
// Check if the number is an Armstrong number
if (sumOfCubes == originalNumber) {
System.out.println("Armstrong Number");
} else {
System.out.println("Not an Armstrong Number");
}
}
}import java.util.Scanner;
- This line imports the
Scannerclass from thejava.utilpackage. TheScannerclass is used to get user input from the console.
public class ArmstrongNumber {
- This line defines a new public class named
ArmstrongNumber. A class in Java is a blueprint from which individual objects are created.
public static void main(String[] args) {
- This is the main method, which is the entry point of any Java program. The
publickeyword means this method can be accessed from outside the class,staticmeans it can be called without creating an instance of the class, andvoidmeans it doesn’t return any value.String[] argsallows the program to accept command-line arguments.
Scanner scanner = new Scanner(System.in);
- This line creates a new
Scannerobject namedscanner.System.inspecifies that theScannershould read input from the keyboard.
System.out.println(“Enter any number: “);
- This line prints the message “Enter any number: “ to the console, prompting the user to input a number.
int number = scanner.nextInt();
- This line reads the integer input from the user and stores it in the variable
number.
int originalNumber = number;
- This line creates a copy of
numbercalledoriginalNumber. This is done so we can later compare it with the sum of the cubes of its digits.
int sumOfCubes = 0;
- This line initializes
sumOfCubesto 0. This variable will hold the sum of the cubes of the digits ofnumber.
while (number > 0) {
- This line starts a
whileloop that continues as long asnumberis greater than 0. The loop is used to process each digit ofnumber.
int digit = number % 10;
- This line extracts the last digit of
numberby using the modulus operator%. For example, ifnumberis 153,digitwill be 3.
number /= 10;
- This line removes the last digit from
numberby performing integer division by 10. After this operation, ifnumberwas 153, it becomes 15.
sumOfCubes += digit * digit * digit;
- This line calculates the cube of
digit(i.e.,digit * digit * digit) and adds it tosumOfCubes.
if (sumOfCubes == originalNumber) {
- This line starts an
ifstatement that checks ifsumOfCubesis equal tooriginalNumber. If they are equal, it meansoriginalNumberis an Armstrong number.
System.out.println(“Armstrong Number”);
- If the condition in the
ifstatement is true, this line prints "Armstrong Number" to the console. - If the
ifcondition is false, this line prints "Not an Armstrong Number" to the console.
In Short:
The program checks if a number is an Armstrong number by:
- Extracting and cubing each digit.
- Summing these cubes.
- Comparing the sum to the original number to determine if they match.
- Time Complexity: O(d), where d is the number of digits in the number.
- Space Complexity: O(1), as only a fixed amount of additional memory is used.
The given approach is both time and space efficient for most practical purposes.
