C Program to Display Armstrong Numbers Between Two Intervals

An Armstrong number (also known as a narcissistic number or pluperfect number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. In this article, we will explore different C Program to Display Armstrong Numbers Between Two Intervals. Each method will include a real example with the solution and output.

Prerequisites

Before diving into the examples, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with loops, conditionals, and functions.
  • A C compiler installed on your system (e.g., GCC).

Simple Method to Find Armstrong Numbers

1.1 Explanation

The simplest method to find Armstrong numbers between two intervals involves iterating through each number in the range, checking if each number is an Armstrong number by calculating the sum of its digits raised to the power of the number of digits.

1.2 Program: Simple Armstrong Number Calculation

This program finds Armstrong numbers between two intervals using a simple loop and conditional statements.

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

int isArmstrong(int num) {
    int originalNum, remainder, n = 0;
    float result = 0.0;

    originalNum = num;
    while (originalNum != 0) {
        originalNum /= 10;
        ++n;
    }

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

    if ((int)result == num)
        return 1;
    else
        return 0;
}

int main() {
    int low, high;

    printf("Enter two numbers(intervals): ");
    scanf("%d %d", &low, &high);

    printf("Armstrong numbers between %d and %d are: ", low, high);
    for (int i = low + 1; i < high; ++i) {
        if (isArmstrong(i)) {
            printf("%d ", i);
        }
    }

    return 0;
}

1.3 Output

C
Enter two numbers(intervals): 100 500
Armstrong numbers between 100 and 500 are: 153 370 371 407

Optimized Method to Find Armstrong Numbers

2.1 Explanation

The optimized method improves upon the simple method by reducing redundant calculations. We pre-calculate the number of digits once and use that to compute the Armstrong condition.

2.2 Program: Optimized Armstrong Number Calculation

This program demonstrates an optimized way to find Armstrong numbers between two intervals.

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

int calculateDigits(int num) {
    return floor(log10(num) + 1);
}

int isArmstrong(int num, int digits) {
    int originalNum = num, remainder;
    float result = 0.0;

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

    return (int)result == num;
}

int main() {
    int low, high;

    printf("Enter two numbers(intervals): ");
    scanf("%d %d", &low, &high);

    printf("Armstrong numbers between %d and %d are: ", low, high);
    for (int i = low + 1; i < high; ++i) {
        int digits = calculateDigits(i);
        if (isArmstrong(i, digits)) {
            printf("%d ", i);
        }
    }

    return 0;
}

2.3 Output

C
Enter two numbers(intervals): 100 500
Armstrong numbers between 100 and 500 are: 153 370 371 407

Recursive Method to Find Armstrong Numbers

3.1 Explanation

A recursive method can be used to find Armstrong numbers by recursively breaking down the problem into smaller sub-problems.

3.2 Program: Recursive Armstrong Number Calculation

This program demonstrates how to use recursion to find Armstrong numbers between two intervals.

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

int calculateDigits(int num) {
    return floor(log10(num) + 1);
}

int isArmstrongRec(int num, int digits) {
    if (num == 0)
        return 0;
    int remainder = num % 10;
    return pow(remainder, digits) + isArmstrongRec(num / 10, digits);
}

int isArmstrong(int num) {
    int digits = calculateDigits(num);
    return num == isArmstrongRec(num, digits);
}

int main() {
    int low, high;

    printf("Enter two numbers(intervals): ");
    scanf("%d %d", &low, &high);

    printf("Armstrong numbers between %d and %d are: ", low, high);
    for (int i = low + 1; i < high; ++i) {
        if (isArmstrong(i)) {
            printf("%d ", i);
        }
    }

    return 0;
}

3.3 Output

C
Enter two numbers(intervals): 100 500
Armstrong numbers between 100 and 500 are: 153 370 371 407

Conclusion

In this article, we explored different C Program to Display Armstrong Numbers Between Two Intervals. We covered a simple method using loops, an optimized method reducing redundant calculations, and a recursive method. Each method was demonstrated with real examples and outputs. Understanding these methods provides a solid foundation for solving similar problems in C programming.

By practicing these examples, you will gain a deeper understanding of loops, conditionals, recursion, and optimization techniques in C.