Armstrong Numbers

Algorithm

Input An integer and number of digits in the integer
Output	Number is Armstrong or not.
Complexity O(d) where d is the number of digits in the integer

Armstrong(num,digits)
1    n=num
2    while(num>0) do
3        digit =n%10;
4        sum=sum+pow(digit,digits)
5        n= n/10;
6   if (sum =num)
7        print it is an Armstrong number
8    Else 
9        print it is not an Armstrong number

Algorithm Description

This algorithm takes an integer check whether it is Armstrong or not.

Step 2. While loop run for each digit of number and find digit^d(where d is the number of digits in the number) and add it to sum, if sum of digit^d=number then number is Armstrong .

C Code


#include<stdio.h>
int main()
{
   int n,i,d,sum=0,m;
   printf("\n enter any integer number  :");
   scanf("%d",&n);
   printf("\n enter number of digits in the number  :");
   scanf("%d",&n);
   m=n;
   while(n>0)
   {
        d=n%10;
        sum+=d*d*d;
        n=n/10;
   }
   if(m==sum)
   {
    	  printf("\ its armstrong number");
   }
   else
   {
	  printf("\ its not armstrong number");
   }
   return 0;     
}