C Program to Print Armstrong Numbers

In this comprehensive blog post, we will delve into the concept of Armstrong numbers and present a C program that identifies and prints Armstrong numbers within a specified range. The article will not only include the complete C program but will also provide detailed explanations of the Armstrong number concept, the logic behind the program, and step-by-step code analysis. Readers will gain insights into the mathematical principles involved and understand how to run the program with sample outputs.

Armstrong Numbers

An Armstrong number (also known as a narcissistic number, pluperfect digital invariant, or pluperfect number) is a number that is the sum of its own digits, each raised to the power of the number of digits. For example, 153 is an Armstrong number because:

13+53+33=1+125+27=153.

C Program Implementation

Let’s start by presenting the C program that identifies and prints Armstrong numbers within a specified range:

#include <stdio.h>
#include <math.h>

// Function to calculate the number of digits in a given 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, remainder, n, result = 0;

originalNum = num;
n = countDigits(num);

while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

return result == num;
}

// Function to print Armstrong numbers in a given range
void printArmstrongNumbers(int start, int end) {
printf("Armstrong numbers in the range %d to %d are:\n", start, end);

for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
printf("%d\n", i);
}
}
}

int main() {
// User input
int start, end;
printf("Enter the range (start end): ");
scanf("%d %d", &start, &end);

// Input validation
if (start >= end) {
printf("Invalid range. Please ensure start < end.\n");
return 1;
}

// Display Armstrong numbers in the specified range
printArmstrongNumbers(start, end);

return 0;
}


Example Usage

Let’s explore how to use the program by running it with sample inputs:

Compile the C program:

gcc armstrong_numbers.c -o armstrong_numbers -lm


Run the compiled program:

./armstrong_numbers


Enter the required information when prompted:

Enter the range (start end): 100 1000

1. The program will identify and display the Armstrong numbers within the specified range.

Example Output

• Example 1:

Enter the range (start end): 100 1000
Armstrong numbers in the range 100 to 1000 are:
153
370
371
407


Example 2:

Enter the range (start end): 1 500
Armstrong numbers in the range 1 to 500 are:
1
2
3
4
5
6
7
8
9
153
370
371
407


Step-by-Step Explanation

1. Function countDigits():

• This function takes an integer num as a parameter and calculates the number of digits in num using a loop.
2. Function isArmstrong():

• This function checks if a given number num is an Armstrong number.
• It calculates the sum of the nth powers of its digits, where n is the number of digits in num.
3. Function printArmstrongNumbers():

• This function takes the start and end of the range as parameters and prints the Armstrong numbers within that range.
4. User Input:

• The program uses scanf() to obtain user input for the range (start and end).
5. Input Validation:

• The program checks if the entered range is valid (i.e., start < end). If not, it prompts the user to enter a valid range.
6. Result Display:

• The program calls the printArmstrongNumbers() function to display Armstrong numbers within the specified range.

Logic Behind the Solution

The program uses mathematical concepts to determine whether a given number is an

Conclusion

This C program provides a comprehensive solution for identifying and printing Armstrong numbers within a specified range. The article has walked through the entire code, explaining the logic, functions, and user input processes. Readers can experiment with different range inputs to observe the program’s output and gain a deeper understanding of Armstrong numbers and their properties. The problem serves as an excellent example of combining mathematical principles with programming to solve an interesting numerical challenge.