Java Program to Display Armstrong Number Between Two Intervals

Java Program to Display Armstrong Number Between Two Intervals

What is  an Armstrong ?

An Armstrong number, also known as a narcissistic number, is a number that is the sum of its own digits when each digit is raised to the power of the number of digits in the number. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371.

 

Algorithm to display Armstrong numbers between two intervals:

  1. Begin a loop that will iterate through the range of numbers between the two intervals.
  2. Inside the loop, create a variable to store the sum of the digits raised to the power of the number of digits. Initialize this variable to 0.
  3. Extract each digit of the current number and raise it to the power of the number of digits. Add the result to the sum.
  4. If the sum is equal to the current number, print the number.
  5. Repeat the loop until all numbers in the range have been checked.

Pseudo  code that implements this algorithm:

public void printArmstrongNumbers(int start, int end) {
  // loop through the range of numbers
  for (int i = start; i <= end; i++) {
    int sum = 0; // initialize the sum

    // extract each digit and add to the sum
    int temp = i;
    int digits = (int) Math.log10(i) + 1;
    while (temp > 0) {
      int digit = temp % 10;
      sum += Math.pow(digit, digits);
      temp /= 10;
    }

    // if the sum is equal to the current number, print it
    if (sum == i) {
      System.out.println(i);
    }
  }
}
 

Here are a few examples of Armstrong numbers:

  • 153 (1^3 + 5^3 + 3^3 = 153)
  • 371 (3^3 + 7^3 + 1^3 = 371)
  • 9474 (9^4 + 4^4 + 7^4 + 4^4 = 9474)

Example :

Run
public class Main {

  public static void main(String[] args) {
    printArmstrongNumbers(100, 999);
  }

  public static void printArmstrongNumbers(int start, int end) {
    // loop through the range of numbers
    for (int i = start; i <= end; i++) {
      int sum = 0; // initialize the sum

      // extract each digit and add to the sum
      int temp = i;
      int digits = (int) Math.log10(i) + 1;
      while (temp > 0) {
        int digit = temp % 10;
        sum += Math.pow(digit, digits);
        temp /= 10;
      }

      // if the sum is equal to the current number, print it
      if (sum == i) {
        System.out.println(i);
      }
    }
  }
}

Output :

153
370
371
407
Here is an explanation of how the program works:
  1. The main method calls the printArmstrongNumbers method, passing it the start and end of the range of numbers to check (100 and 999, respectively).
  2. The printArmstrongNumbers method begins a loop that iterates through the range of numbers from start to end.
  3. Inside the loop, a variable sum is initialized to 0. This variable will be used to store the sum of the digits raised to the power of the number of digits.
  4. The number of digits in the current number is calculated using Math.log10 and + 1. This is stored in the digits variable.
  5. A while loop is used to extract each digit of the current number, raise it to the power of digits, and add it to the sum.
  6. After all the digits have been processed, the program checks if the sum is equal to the current number. If it is, the number is printed to the console.
  7. The loop continues until all numbers in the range have been checked.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription