Check if a given 3 digit number is Armstrong - C

C program to check entered number is Armstrong number or not. (For 3-digit number)

Solution:

Armstrong number is a number which sum of the cube of its digits is equals to that number.
Ex. 153= 1^3+5^3+3^3 =153. Other Armstrong numbers are: 370,371,407.

General definition - Those number which sum of its digits to power of number of its digits is equal to that number are known as Armstrong number.
Ex. 1634
Total digit in 1634 are 4.
1^4+6^4+3^4+4^4 = 1+1296+81+64 =1634

#include<stdio.h>
int main()
{
      int s=0,l;
      long n,t;
      printf("Enter Three Digit Number : ");
      scanf("%ld",&n);
      t=n;
      while(n>0) //Here, we can use (n!=0)
      {
            l=n%10;
            s=s+(l*l*l);
            n=n/10;
      }
      if(s==t)
            printf("%ld Armstrong Number",t);
      else
            printf("%ld Not Armstrong Number",t);
      return 0;
}


Output:

armstrong number

C program to check entered number is Armstrong number or not. (For any digit number)

Solution:

#include <stdio.h>
#include <math.h>
int main()
{
      int num, t, r, s = 0, count = 0 ;
      printf("Enter Number : ");
      scanf("%d", &num);
      t = num;
      while (t>0)
      {
            t=t/10;
            ++count;
      }
      t = num;
      while (t > 0)
      {
            r = t%10;
            s=s+ pow(r, count);
            t=t/10;
      }
      if(s == num)
            printf("\n%d Armstrong Number.", num);
      else
            printf("\n%d NOT Armstrong Number.", num);
      return 0;
}


Output:

armstrong number