Dremendo Tag Line

Input a number and check it is an armstrong number or not using while loop in C

while Loop - Question 15

In this question, we will see how to input a number and check if it is an armstrong numbers or not in C programming using while loop. To know more about while loop click on the while loop lesson.

Q15) Write a program in C to input a number and check if it is an armstrong numbers or not using while loop.

An Armstrong number is a number whose sum of cube of the digits is equal to the original number. Example 153 = 13 + 53 + 33

Program

#include <stdio.h>
#include <conio.h>

int main()
{
    int n,d,on,soc=0;

    printf("Enter any number ");
    scanf("%d",&n);
    on=n;			// storing the original number

    while(n>0)
    {
        d=n%10;
        soc=soc+(d*d*d);		// sum of cube of the digits
        n=n/10;
    }

    if(on==soc)
    {
        printf("Armstrong Number");
    }
    else
    {
        printf("Not Armstrong Number");
    }
    return 0;
}

Output

Enter any number 153
Armstrong Number
video-poster