Write a C-Program to Check that a given number is an Armstrong Number.

0
35

A positive integer is called an Armstrong number of order n if

abcd... = an + bn + cn + dn + ...

In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example:

153 = 1*1*1 + 5*5*5 + 3*3*3  // 153 is an Armstrong number.
Example to check Armstrong Number of three digits:

#include <stdio.h>
    int main()
    {
        int number, originalNumber, remainder, result = 0;
        printf("Enter a three digit integer: \n");
        scanf("%d", &number);
        originalNumber = number;
        while (originalNumber != 0)
        {
            remainder = originalNumber%10;
            result += remainder*remainder*remainder;
            originalNumber /= 10;
        }
        if(result == number)
            printf("%d is an Armstrong number.",number);
        else
            printf("%d is not an Armstrong number.",number);
        return 0;
    }
Output:

Enter a three-digit integer: 407
407 is an Armstrong number.

LEAVE A REPLY

Please enter your comment!
Please enter your name here