C program to check if given number is armstrong or not

05 min read

C program to check if a given number is Armstrong or not is shown below. Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers, which means 1^3+5^3+3^3=153. So to implement this logic, we need to take out each digit of the given number and find the cube of each digit and at the end add them together to see if it is Armstrong or not.

#include <stdio.h>
int main()
{
    int number, originalNumber, remainder, result = 0;

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

    originalNumber = number;

    while (originalNumber != 0)
    {
        remainder = originalNumber%10;                         //take out each digit of the given number
        result += remainder*remainder*remainder;        //cube each of the digits and add them
        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: 123 123 is not an Armstrong number.

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error