C program to print all Armstrong numbers from 1 to n

Category: C Program

Learn how to write a C program to print all Armstrong numbers from 1 to n. This comprehensive guide includes an explanation, algorithm, and complete code example.

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its 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.

In this article, we will write a C program to find and print all Armstrong numbers from 1 to a user-specified number n. We'll start by understanding the concept of Armstrong numbers in more detail, then outline the algorithm, and finally provide the complete C program with an explanation.

Understanding Armstrong Numbers

An Armstrong number of n digits is an integer such that the sum of its digits raised to the n-th power equals the number itself. For example:

  • 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
  • 9474 is an Armstrong number because 9^4 + 4^4 + 7^4 + 4^4 = 9474.

Algorithm to Find Armstrong Numbers from 1 to n

  1. Input the number (n): Get the upper limit from the user.
  2. Iterate through each number from 1 to (n).
  3. Count the digits in the current number.
  4. Calculate the sum of powers of its digits.
  5. Check if the sum is equal to the original number.
  6. Print the number if it is an Armstrong number.

Write a C program to print all Armstrong numbers from 1 to n

Here's the complete C program to print all Armstrong numbers from 1 to n:

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

int main() {
    int n, num, originalNum, remainder, result, numDigits;

    // Input the upper limit from the user
    printf("Enter an upper limit: ");
    scanf("%d", &n);

    printf("Armstrong numbers between 1 and %d are:\n", n);

    // Iterate through each number from 1 to n
    for (num = 1; num <= n; num++) {
        originalNum = num;
        result = 0;
        numDigits = 0;
        int temp = num;

        // Count the number of digits in the number
        while (temp != 0) {
            temp /= 10;
            numDigits++;
        }

        temp = num;

        // Calculate the sum of the nth powers of each digit
        while (temp != 0) {
            remainder = temp % 10;               // Extract the last digit
            result += pow(remainder, numDigits); // Raise the digit to the power of numDigits and add to result
            temp /= 10;                          // Remove the last digit
        }

        // Check if the number is an Armstrong number
        if (result == num) {
            printf("%d\n", num);
        }
    }

    return 0;
}

Output

Enter an upper limit: 1000
Armstrong numbers between 1 and 1000 are:
1
2
3
4
5
6
7
8
9
153
370
371
407

Explanation of the Code

  1. Input the Upper Limit:
    • scanf("%d", &n); reads the upper limit n from the user.
  2. Iterate through Each Number:
    • The for loop iterates through each number from 1 to n.
  3. Count the Number of Digits:
    • A while loop counts the number of digits in the current number by repeatedly dividing by 10.
  4. Calculate the Sum of Powers:
    • Another while loop calculates the sum of each digit raised to the power of the number of digits.
  5. Check for Armstrong Number:
    • If the calculated sum is equal to the original number, it is printed as an Armstrong number.

Recommended Posts