![]() |
VOOZH | about |
Write a C program to find and display all Armstrong numbers between two given intervals.
An Armstrong number (also known as a narcissistic number or pluperfect number) of a given number of digits 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 because 1^3 + 5^3 + 3^3 = 153.
Input: Lower limit = 100, Upper limit = 500
Output: 153, 370, 371, 407Input: Lower limit = 1, Upper limit = 1000
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407
We will use three approaches to solve this problem:
Table of Content
Idea
The iterative approach involves iterating through each number in the given range, checking if it is an Armstrong number by:
Steps
Code
Armstrong numbers between 100 and 500 are: 153 370 371 407
Complexity
Idea
The mathematical approach uses logarithms to count the number of digits in a number. This can be more efficient for larger ranges. The approach still involves computing the sum of each digit raised to the power of the number of digits and comparing it with the original number.
Steps
Code
Output:
153, 370, 371, 407Complexity
Idea
This approach involves extracting each digit of the number, raising it to the power of the number of digits, and summing these values. This method is straightforward and easy to understand.
Steps
Code
Output:
153, 370, 371, 407Complexity
Note: If you face undefined reference to 'pow()' error, please refer to this post for solution.