C Program For Checking The Armstrong Number

The sum of the cubes of a number is called an Armstrong number if it equals the number itself. The C program below checks whether the input number is Armstrong or not.

int main()
   int num,copy_of_num,sum=0,rem;

   //Store input number in variable num
   printf("\nEnter a number:");

   /* Value of variable num would change in the
      below while loop so we are storing it in 
      another variable to compare the results 
      at the end of program
   copy_of_num = num;

   /* We are adding cubes of every digit
    * and storing the sum in variable sum
   while (num != 0)
      rem = num % 10;
      sum = sum + (rem*rem*rem);
      num = num / 10;

   /* If sum of cubes of every digit is equal to number
    * itself then the number is Armstrong
   if(copy_of_num == sum)
      printf("\n%d is an Armstrong Number",copy_of_num);
      printf("\n%d is not an Armstrong Number",copy_of_num);


Enter a number: 370
370 is an Armstrong Number

You can check the outcome by doing the following:

370 = 3*3*3 + 7*7*7 + 0*0*0
    = 27 + 343 + 0
    = 370

As you can see, the sum of the digits of the number 370 equals the number itself.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button