To Check Armstrong Number in Java

Yuvaraj
3 min readAug 8, 2024

--

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

Armstrong Number Calculation for 153:

  1. Count the digits: 153 has 3 digits.
  2. 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 Scanner class from the java.util package. The Scanner class 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 public keyword means this method can be accessed from outside the class, static means it can be called without creating an instance of the class, and void means it doesn’t return any value. String[] args allows the program to accept command-line arguments.

Scanner scanner = new Scanner(System.in);

  • This line creates a new Scanner object named scanner. System.in specifies that the Scanner should 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 number called originalNumber. This is done so we can later compare it with the sum of the cubes of its digits.

int sumOfCubes = 0;

  • This line initializes sumOfCubes to 0. This variable will hold the sum of the cubes of the digits of number.

while (number > 0) {

  • This line starts a while loop that continues as long as number is greater than 0. The loop is used to process each digit of number.

int digit = number % 10;

  • This line extracts the last digit of number by using the modulus operator %. For example, if number is 153, digit will be 3.

number /= 10;

  • This line removes the last digit from number by performing integer division by 10. After this operation, if number was 153, it becomes 15.

sumOfCubes += digit * digit * digit;

  • This line calculates the cube of digit (i.e., digit * digit * digit) and adds it to sumOfCubes.

if (sumOfCubes == originalNumber) {

  • This line starts an if statement that checks if sumOfCubes is equal to originalNumber. If they are equal, it means originalNumber is an Armstrong number.

System.out.println(“Armstrong Number”);

  • If the condition in the if statement is true, this line prints "Armstrong Number" to the console.
  • If the if condition 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.

--

--