Home

A program to find whether given number is an ARMSTRONG number

C Programming Language 027

The following code shows how to write a program to find whether given number is an ARMSTRONG number or not.

Program

#include <stdio.h>

void main()
{
    int r, n, temp, sum = 0;
    printf("Enter a number: ");
    scanf("%d", &n);

    temp = n;
    while (n > 0)
    {
        r = n % 10;
        sum += r * r * r;
        n = n / 10;
    }

    if (sum == temp)
    {
        printf("Armstrong number");
    }
    else
    {
        printf("Not an armstrong number");
    }

    printf("\n");
}

Result

Enter a number: 45
Not an armstrong number
Enter a number: 153
Armstrong number


Last Updated on

Next Post: Rules vs Guidelines →

Comments