Anonymous

How Do I Write A Program In C/C++ To Check A Number Is Armstrong No Or Not?

3

3 Answers

Anonymous Profile
Anonymous answered
#include
#include
void main()
{
int n,r,sum=0,m;
clrscr();
printf("enter the number");
scanf("%d",&n);
n=m;
while(n>0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(sum==m)
printf("no. Is armstrong");
else
printf("not armstrong");
getch();
}
John Yows Profile
John Yows answered
There is an example of such a program for 3-digit numbers in the January 8th response at www.geekinterview.com    Not elegant, but looks like it would work. It should not be difficult to expand this to allow other lengths of inputs (i.e. 4 digit or larger numbers), though the method of raising a digit to the nth power should be changed from the "NxNxN" method used in the example and you would have to change the way the individual digits of the input are extracted.    Basically, you first need to separate the digits in the original number into different variables (e.g. If given 3456, set variables i=3, j=4, k=5 and l=6). Then you have to raise each of these values to the power of ... However many digits were in the original number (in this example, 4) and add the results together, then compare that to the sum of the individual digits.    Does that help, or were you looking for complete code for a program that would handle variable-length input?
John Peter Profile
John Peter answered
You know the concept of armstrong number..
For example consider 153. The sum of cubes of the digits of 153 that is 1^3+5^3+3^3=153. This is the way you decide a no is armstrong or not

Answer Question

Anonymous