This program identifies Armstrong numbers within a range specified by the user. An Armstrong number is a number that equals the sum of its digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1³ + 5³ + 3³ equals 153. This guide shows multiple methods to calculate and print all Armstrong numbers in a range using C.
Understanding The Problem
The program should take two numbers from the user as the lower and upper bounds of a range and print all Armstrong numbers within that range. An Armstrong number is determined by calculating the sum of its digits raised to the power of the number of digits and comparing it to the original number. Handling multiple numbers efficiently is key.
Steps to Solve the Problem:
- Take the lower and upper limits as input from the user.
- Loop through each number in the range.
- Count the number of digits in the number.
- Calculate the sum of the digits raised to the power of the number of digits.
- Compare the sum with the original number and print it if they match.
Solution 1: Using Loops
This is the standard method. The program loops through each number in the range, counts the number of digits, calculates the sum of the digits raised to the power of the number of digits using pow()
, and prints the number if the sum equals the original.
#include <stdio.h>
#include <math.h>
int main() {
int lower, upper, num, originalNum, remainder, n;
double sum;
printf("Enter lower and upper range: ");
scanf("%d %d", &lower, &upper);
printf("Armstrong numbers between %d and %d are: ", lower, upper);
for(num = lower; num <= upper; num++) {
originalNum = num;
sum = 0;
n = 0;
while(originalNum != 0) {
originalNum /= 10;
n++;
}
originalNum = num;
while(originalNum != 0) {
remainder = originalNum % 10;
sum += pow(remainder, n);
originalNum /= 10;
}
if((int)sum == num) {
printf("%d ", num);
}
}
return 0;
}
This method is easy to understand and works well for small to medium ranges.
Solution 2: Using a Function
Creating a function improves readability and reusability. The isArmstrong()
function checks whether a number is an Armstrong number.
#include <stdio.h>
#include <math.h>
int isArmstrong(int num) {
int originalNum = num, remainder, n = 0;
double sum = 0;
while(originalNum != 0) {
originalNum /= 10;
n++;
}
originalNum = num;
while(originalNum != 0) {
remainder = originalNum % 10;
sum += pow(remainder, n);
originalNum /= 10;
}
return (int)sum == num;
}
int main() {
int lower, upper;
printf("Enter lower and upper range: ");
scanf("%d %d", &lower, &upper);
printf("Armstrong numbers between %d and %d are: ", lower, upper);
for(int num = lower; num <= upper; num++) {
if(isArmstrong(num)) {
printf("%d ", num);
}
}
return 0;
}
This approach is cleaner and allows you to call the function multiple times in different contexts.
Solution 3: Using Recursion
Recursion can be used to calculate the sum of digits raised to the power of n
. This method is more for learning purposes than efficiency.
#include <stdio.h>
#include <math.h>
double sumOfPowers(int num, int n) {
if(num == 0)
return 0;
return pow(num % 10, n) + sumOfPowers(num / 10, n);
}
int isArmstrong(int num) {
int temp = num, n = 0;
while(temp != 0) {
temp /= 10;
n++;
}
return sumOfPowers(num, n) == num;
}
int main() {
int lower, upper;
printf("Enter lower and upper range: ");
scanf("%d %d", &lower, &upper);
printf("Armstrong numbers between %d and %d are: ", lower, upper);
for(int num = lower; num <= upper; num++) {
if(isArmstrong(num)) {
printf("%d ", num);
}
}
return 0;
}
Recursion here demonstrates an alternative way to calculate digit powers, but it is less efficient for very large numbers.
FAQs
Q1: Can Armstrong numbers be negative?
No, Armstrong numbers are defined only for non-negative integers.
Q2: What is the difference between an Armstrong number and a palindrome?
An Armstrong number depends on the sum of digits raised to the power of the number of digits, while a palindrome depends on the digits being the same forwards and backwards.
Q3: Can this program handle decimal numbers?
No, Armstrong numbers are strictly integers, so decimal inputs are not considered.
Q4: Which method is best?
For most purposes, using loops or a dedicated function is efficient and readable. Recursion is mostly for learning and demonstration.
Conclusion
This program demonstrates loops, functions, and recursion to find Armstrong numbers in a range. Using a function makes the code reusable and easier to maintain, while recursion offers a conceptual alternative. The same logic can be adapted for larger ranges or other number-based problems.
References & Additional Resources
- C Programming Tutorial – Beginner-friendly tutorials covering loops, functions, and arithmetic operations.
- GeeksforGeeks – Armstrong Number – Provides multiple examples and solutions for finding Armstrong numbers.
- TutorialsPoint – C Math Library – Reference for
pow()
and other mathematical functions in C.