Published on : 5th Oct 2021

Armstrong number checking in C programming
What is armstrong number ?
An armstrong number is a number that is equal to the sum of cube of its digits. Example : 0, 1, 370, 407 etc.
Source code :
#include <stdio.h> int power(int a, int b) { int i; int s = 1; for(i=0; i<b; i++) { s *= a; } return s; } int armstrong(int num) { int sum = 0; int temp = num; while(num > 0) { int d = num % 10; num /= 10; sum += power(d, 3); } return sum == temp; } int main(int argc, char** argv) { int num; printf("Enter a number : "); scanf("%d", &num); int is_armstrong = armstrong(num); if(is_armstrong) { printf("%d is an armstrong number.", num); } else { printf("%d is not an armstrong number.", num); } return 0; }
Output :
Enter a number : 407 407 is an armstrong number.
Explanation :
We are slicing the digits of the given number one by one, doing cube of thg digit and adding to the sum. Finally, checking the sum is equal to the numner or not. The result is saying that whether the number is armstrong or not.