Sunday, 2 October 2011

Program to generate Armstrong number in C


/* Armstrong number Eg if abc is a number then if (a^3+b^3+c^3=abc) then number is armstrong now
Let number is abcd the if (a^4+b^4+c^4+d^4=abcd)  then number is armstron. 
Note: Power to each digit must be equal to number of digits of the given number  */


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i=0,s=0,t=0,r=0,k=1;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);              
for(k=1;k<n;k++)
{
     i=k;
     while(i>0)
    {
     i=i/10;
     t++;
    }
for(i=k;i>0;i=i/10)
{
     r=i%10;
     s= s+pow(r,t);
}
if(s==k)
printf(" %d ",k);
}
getch();
}




/*for example enter n=10000//
output will be 1  2  3  4  5  6  7  8  9  153  370  371  407  1634  8208        9474
these are Armstrong number between 1 to 10000
verification 1^1=1 similarly for 153; 1^3+5^3+3^3=1+125+27=153 
and for 1634; 1^4+6^4+3^4+4^4=1+1296+81+256=1634*/

No comments:

Post a Comment