Draw a flowchart that checks if a number is an armstrong number or not.

C Program to Check Armstrong Number

✅ C Program to Check Armstrong Number (Without Using pow Function)

An Armstrong number is a number that is equal to the sum of its digits raised to the power of the total number of digits.

Example:
153 → 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 ✔

💻 C Source Code (Without math.h)

#include <stdio.h>

int main() {
    int n, originalNumber, remainder, digits = 0;
    int result = 0;

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

    originalNumber = n;

    // Count number of digits
    while (originalNumber != 0) {
        originalNumber /= 10;
        digits++;
    }

    originalNumber = n;

    // Calculate Armstrong sum without pow()
    while (originalNumber != 0) {
        remainder = originalNumber % 10;

        int power = 1;
        for(int i = 0; i < digits; i++) {
            power *= remainder;
        }

        result += power;
        originalNumber /= 10;
    }

    // Check Armstrong condition
    if (result == n)
        printf("%d is an Armstrong number.\n", n);
    else
        printf("%d is not an Armstrong number.\n", n);

    return 0;
}

🔎 Explanation

  • The user enters a number.
  • The program counts total digits.
  • Each digit is extracted using % 10.
  • Instead of pow(), a for loop multiplies the digit repeatedly to calculate its power.
  • The sum is compared with the original number.
  • If equal → Armstrong number, otherwise not.

🧪 Sample Output

Enter a number: 153
153 is an Armstrong number.
Enter a number: 123
123 is not an Armstrong number.

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন