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.
Examples
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
Approaches to Display Armstrong Number Between Two Intervals in C
We will use three approaches to solve this problem:
Table of Content
1. Iterative Approach
Idea
The iterative approach involves iterating through each number in the given range, checking if it is an Armstrong number by:
- Counting the number of digits.
- Computing the sum of each digit raised to the power of the number of digits.
- Comparing the computed sum with the original number.
Steps
- Define the range in the driver code.
- Iterate through each number in the given range.
- For each number, count the number of digits.
- Compute the sum of the digits each raised to the power of the number of digits.
- If the computed sum equals the original number, print the number.
Code
#include <stdio.h>
// Function to count the number of digits in a number
int countDigits(int num) {
int count = 0;
while (num != 0) {
num /= 10;
++count;
}
return count;
}
// Function to compute the power of a number manually
int power(int base, int exp) {
int result = 1;
for (int i = 0; i < exp; ++i) {
result *= base;
}
return result;
}
// Function to check if a number is an Armstrong number
int isArmstrong(int num) {
int originalNum = num;
int n = countDigits(num);
int result = 0;
while (num != 0) {
int remainder = num % 10;
result += power(remainder, n);
num /= 10;
}
return (result == originalNum);
}
int main() {
int low = 100, high = 500; // Define the range in the driver code
printf("Armstrong numbers between %d and %d are: ", low, high);
// Iterate through each number in the interval
for (int i = low + 1; i < high; ++i) {
if (isArmstrong(i)) {
printf("%d ", i);
}
}
return 0;
}
Output
Armstrong numbers between 100 and 500 are: 153 370 371 407
Complexity
- Time Complexity: O(nâ‹…d), where n is the number of numbers in the given range and d is the number of digits in the number.
- Auxiliary Space: O(1), since we use a constant amount of space.
2. Mathematical Approach using Logarithms
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
- Define the range in the driver code.
- Iterate through each number in the given range.
- For each number, use logarithms to count the number of digits.
- Compute the sum of the digits each raised to the power of the number of digits.
- If the computed sum equals the original number, print the number.
Code
#include <stdio.h>
#include <math.h>
// Function to check if a number is an Armstrong
//cnumber using logarithms
int isArmstrong(int num) {
int originalNum, remainder, result = 0;
int n;
// Calculate number of digits using logarithms
originalNum = num;
n = log10(num) + 1;
// Compute the sum of each digit raised to
// the power of n
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
// Check if the result is equal to the original
// number
return (result == num);
}
int main() {
int low = 1, high = 1000;
printf("Armstrong numbers between %d and %d are: ", low, high);
// Iterate through each number in the interval
for (int i = low + 1; i < high; ++i) {
if (isArmstrong(i)) {
printf("%d ", i);
}
}
return 0;
}
Output:
153, 370, 371, 407Complexity
- Time Complexity: O(nâ‹…d), similar to the iterative approach, where n is the number of numbers in the given range and d is the number of digits in the number.
- Auxiliary Space: O(1), since we use a constant amount of space.
3. Digit-by-Digit Extraction Approach
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
- Define the range in the driver code.
- Iterate through each number in the given range.
- For each number, count the number of digits.
- Extract each digit, raise it to the power of the number of digits, and sum these values.
- If the computed sum equals the original number, print the number.
Code
#include <stdio.h>
#include <math.h>
// Function to count the number of digits
// in a number
int countDigits(int num) {
int count = 0;
while (num != 0) {
num /= 10;
++count;
}
return count;
}
// Function to check if a number is an
// Armstrong number
int isArmstrong(int num) {
int originalNum = num;
int n = countDigits(num);
int result = 0;
while (num != 0) {
int remainder = num % 10;
result += pow(remainder, n);
num /= 10;
}
return (result == originalNum);
}
int main() {
int low = 100, high = 500;
printf("Armstrong numbers between %d and %d are: ",
low, high);
// Iterate through each number in the interval
for (int i = low + 1; i < high; ++i) {
if (isArmstrong(i)) {
printf("%d ", i);
}
}
return 0;
}
Output:
153, 370, 371, 407Complexity
- Time Complexity: O(nâ‹…d), where n is the number of numbers in the given range and d is the number of digits in the number.
- Auxiliary Space Complexity: O(1), since we use a constant amount of space.
Note: If you face undefined reference to 'pow()' error, please refer to this post for solution.