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


3 Answers

Anonymous Profile
Anonymous answered
void main()
int n,r,sum=0,m;
printf("enter the number");
printf("no. Is armstrong");
printf("not armstrong");
John Yows Profile
John Yows answered
There is an example of such a program for 3-digit numbers in the January 8th response at    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