C Program to Check Armstrong Number

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 article explores various C Program to Check Armstrong Number. Each method is illustrated with a real example, solution, and output.

Prerequisites

Before delving 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 Check Armstrong Number

1.1 Explanation

The simplest method to check an Armstrong number involves calculating the number of digits, then raising each digit to the power of the number of digits, and summing these values to see if they equal the original number.

1.2 Program: Simple Armstrong Number Check

This program checks if a given number is an Armstrong number using a straightforward 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;
    }

    return ((int)result == num);
}

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (isArmstrong(num))
        printf("%d is an Armstrong number.\n", num);
    else
        printf("%d is not an Armstrong number.\n", num);

    return 0;
}

1.3 Output

C
Enter a number: 153
153 is an Armstrong number.

Optimized Method to Check Armstrong Number

2.1 Explanation

The optimized method reduces redundant calculations by pre-calculating the number of digits and using that value in the power calculation loop.

2.2 Program: Optimized Armstrong Number Check

This program demonstrates an optimized way to check if a number is an Armstrong number.

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

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

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

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

    return ((int)result == num);
}

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (isArmstrong(num))
        printf("%d is an Armstrong number.\n", num);
    else
        printf("%d is not an Armstrong number.\n", num);

    return 0;
}

2.3 Output

C
Enter a number: 370
370 is an Armstrong number.

Recursive Method to Check Armstrong Number

3.1 Explanation

A recursive method can be used to check if a number is an Armstrong number by recursively breaking down the problem into smaller sub-problems.

3.2 Program: Recursive Armstrong Number Check

This program demonstrates how to use recursion to check if a number is an Armstrong number.

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 num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (isArmstrong(num))
        printf("%d is an Armstrong number.\n", num);
    else
        printf("%d is not an Armstrong number.\n", num);

    return 0;
}

3.3 Output

C
Enter a number: 407
407 is an Armstrong number.

Conclusion

In this article, we explored different C Program to Check Armstrong Number.