Uncovering the Secrets of Armstrong Numbers

Have you ever wondered about the mystical world of numbers? Specifically, those enigmatic integers known as Armstrong numbers? To grasp the concept, you’ll need a solid foundation in C programming, including if...else statements and while and do...while loops.

What Makes an Armstrong Number Tick?

An Armstrong number of order n is a positive integer with a unique property: the sum of cubes of each digit equals the number itself. Take, for instance, the three-digit Armstrong number 153. When you cube each digit (1³ + 5³ + 3³) and add them up, you get… 153! This intriguing phenomenon has captivated mathematicians for centuries.

Cracking the Code: A Step-by-Step Guide

So, how do we identify Armstrong numbers? Let’s break it down:

  • Counting Digits: First, we calculate the number of digits in the given integer and store it in the variable n.
  • Raising to Power: In each iteration of the second for loop, we compute the power of individual digits.

Putting it into Practice: Checking Armstrong Numbers

Now, let’s see this concept in action! Here’s a program that checks whether a given number is an Armstrong number of three digits:


#include <stdio.h>

int main() {
    int num, sum = 0, temp, remainder, n = 0;

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

    temp = num;

    while (temp!= 0) {
        remainder = temp % 10;
        sum += remainder * remainder * remainder;
        temp /= 10;
    }

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

    return 0;
}

And what about Armstrong numbers of n digits? We’ve got you covered:


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

int main() {
    int num, sum = 0, temp, remainder, n = 0;

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

    temp = num;

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

    temp = num;

    while (temp!= 0) {
        remainder = temp % 10;
        sum += pow(remainder, n);
        temp /= 10;
    }

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

    return 0;
}

Leave a Reply