C Programming Code Examples C > For Loops and While Loops Code Examples C program to check whether a number is armstrong number or not C program to check whether a number is armstrong number or not Write a C program to input a number from user and check whether given number is Armstrong number or not. How to check Armstrong numbers in C program. What is Armstrong number? An Armstrong number is a n-digit number that is equal to the sum of the nth power of its digits. For example - 6 = 6^1 = 6 371 = 3^3 + 7^3 + 1^3 = 371 Input a number from user. Store it in some variable say j. Make a temporary copy of the value to some another variable for calculation purposes, say originalNum = j. Count total digits in the given number, store result in a variable say digits. Initialize another variable to store the sum of power of its digits ,say sum = 0. Run a loop till j > 0. The loop structure should look like while(j > 0). Inside the loop, find last digit of j. Store it in a variable say lastDigit = j % 10. Now comes the real calculation to find sum of power of digits. Perform sum = sum + pow(lastDigit, digits). Since the last digit of j is processed. Hence, remove last digit by performing j = j / 10. After loop check if(originalNum == sum), then it is Armstrong number otherwise not. /** * C program to check Armstrong number */ #include <stdio.h> #include <math.h> int main() { int originalNum, j, lastDigit, digits, sum; /* Input number from user */ printf("Enter any number to check Armstrong number: "); scanf("%d", &j); sum = 0; /* Copy the value of j for processing */ originalNum = j; /* Find total digits in j */ digits = (int) log10(j) + 1; /* Calculate sum of power of digits */ while(j > 0) { /* Extract the last digit */ lastDigit = j % 10; /* Compute sum of power of last digit */ sum = sum + round(pow(lastDigit, digits)); /* Remove the last digit */ j = j / 10; } /* Check for Armstrong number */ if(originalNum == sum) { printf("%d is ARMSTRONG NUMBER", originalNum); } else { printf("%d is NOT ARMSTRONG NUMBER", originalNum); } return 0; }