C Program to Display Armstrong Number Between Two Intervals

Updated on September 30, 2024
Display Armstrong Number Between Two Intervals header image

Introduction

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. This intriguing property makes Armstrong numbers a popular subject in computer programming, especially when learning to manipulate digits.

In this article, you will learn how to write a C program that displays all Armstrong numbers between two given intervals. You'll see examples that demonstrate how to determine if a number is an Armstrong number and how to iterate through a range to find all such numbers efficiently.

Verify Armstrong Number

Determine if a Single Number is an Armstrong Number

  1. Break down the number to get individual digits.

  2. Sum the powers of these digits, with the power equal to the number of digits.

    c
    #include <stdio.h>
    #include <math.h>
    
    int is_armstrong(int num) {
        int original = num, sum = 0;
        int n = floor(log10(num)) + 1; // Number of digits
        while (num > 0) {
            int digit = num % 10;
            sum += pow(digit, n);
            num /= 10;
        }
        return sum == original;
    }
    

    This function is_armstrong checks if the number num is an Armstrong number. It first calculates the number of digits (n). Then, it decomposes the number into individual digits, raising them to the power n and accumulating the sum. Finally, it compares the result against the original number.

Loop Through an Interval to Find All Armstrong Numbers

  1. Iterate through all numbers in the interval.

  2. Apply the Armstrong check to each number and print it if it matches.

    c
    #include <stdio.h>
    #include <math.h>
    
    int is_armstrong(int num);
    
    void find_armstrong_in_range(int lower, int upper) {
        for (int i = lower; i <= upper; i++) {
            if (is_armstrong(i)) {
                printf("%d is an Armstrong number\n", i);
            }
        }
    }
    
    int main() {
        int lower, upper;
        printf("Enter the lower bound: ");
        scanf("%d", &lower);
        printf("Enter the upper bound: ");
        scanf("%d", &upper);
    
        find_armstrong_in_range(lower, upper);
        return 0;
    }
    

    In this script, the function find_armstrong_in_range() loops through each number between the lower and upper bounds, checks whether it is an Armstrong number using the is_armstrong() function, and prints it if it is. The main() function gathers the range inputs from the user.

Conclusion

The challenge of displaying Armstrong numbers between two intervals in C teaches you not only about working with numerical logic but also gives you hands-on experience with functions, loops, conditionals, and basic input/output in C. By implementing the methods discussed above, you enhance your understanding of both C programming and mathematical concepts. Use this knowledge to explore further into number theory and algorithm optimization in your future projects.