Wednesday, 6 April 2016

How To Identify Armstrong Numbers Using Turbo C Compiler

According to illuminations.nctm.org, an Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.
To have a clearer understanding of what an Armstrong number is, I have prepared below a solution on how to determine if a number is an Armstrong or not.

#include<math.h>

#include<stdio.h>
void armstrong(void);
main()
{
clrscr();
armstrong();
getch();
}
void armstrong(void)
{
long count=0,m,n,m1,q,r,a,b,sum=0;
scanf("%ld",&n);
m=n;
m1=n;
for(;n!=0;)
{
r=n/10;
count++;
n=r;
}
for(;m!=0;)
{
a=m/10;
b=m%10;
sum=sum+ pow(b,count);
m=a;
}
if(sum==m1)
{
printf("Armstrong");
}
else
{
printf("Not");
}
}


No comments:

Post a Comment