Tutorialspoint
Problem
Solution
Submissions

Print All Armstrong Numbers in a Given Range

Certification: Basic Level Accuracy: 67.31% Submissions: 52 Points: 10

Write a Python program that finds all Armstrong numbers in a given range.

Example 1
  • Input: start = 100, end = 1000
  • Output: [153, 370, 371, 407]
  • Explanation:
    • Step 1: For each number in the range [100, 1000], check if it's an Armstrong number.
    • Step 2: For 153: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 ✓
    • Step 3: For 370: 3³ + 7³ + 0³ = 27 + 343 + 0 = 370 ✓
    • Step 4: For 371: 3³ + 7³ + 1³ = 27 + 343 + 1 = 371 ✓
    • Step 5: For 407: 4³ + 0³ + 7³ = 64 + 0 + 343 = 407 ✓
    • Step 6: No other Armstrong numbers exist in this range.
Example 2
  • Input: start = 1, end = 10
  • Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
  • Explanation:
    • Step 1: For single-digit numbers, each number is an Armstrong number.
    • Step 2: For example, 9: 9¹ = 9 ✓
    • Step 3: All numbers from 1 to 9 are Armstrong numbers.
Constraints
  • 1 ≤ startend ≤ 10^6
  • Time Complexity: O(n * log(n))
  • Space Complexity: O(k) where k is the number of Armstrong numbers found
NumberControl StructuresGoldman SachsAdobe
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Calculate the number of digits in each number
  • Use a loop to extract each digit and compute the sum of powers

The following are the steps to solve:

  • Define a function that accepts a range (start, end).
  • Iterate through each number in that range.
  • Convert the number to digits (ex: 153 : [1, 5, 3]).
  • Find the number of digits (this becomes the power).
  • Calculate the sum of each digit raised to the power.
  • Check if the sum equals the original number.
  • If yes, it is an Armstrong number, add it to the result list.
  • Return the final list of Armstrong numbers.

Submitted Code :