Armstrong numbers are the sum of their own digits to the power of the number of digits

For example, 153 = 1³ + 5³ + 3³

Here is my attempt

#include <stdio.h>
#include <conio.h>

void main()

{
clrscr();

int i,r,h,t,o,s,n;

printf("Please enter any integer.(Not more than four-digits) ");
scanf("%d" , &i);

if (i-1000>=0) (n=4);
if (i-1000<0 && i-100>=0) (n=3);
if (i-100<0 && i-10>=0) (n=2);
if (i-10<0 && i-1>=0) (n=1);

r=i/1000;
h=(i-1000*r)/100;
t=(i-1000*r-100*h);
o=(i-1000*r-100*h-10*t);


s=r^n+h^n+t^n+i^n+o^n;

if (i==s) printf("It is an Armstrong number. ");
else printf("It is not an Armstrong number. ");

getch();

}

I understand that

s=r^n+h^n+t^n+i^n+o^n;

is wrong,how do i proceed then ?

Line 14 - 17 what????

Have you not heard of else if

if (i >= 1000)
{
    n = 4;
}
else if (i >= 100)
{
    n = 3;
}
...

Line 19 - 22 what ???
Have you not heard of the modulus operator %
Line 21 (which is wrong anyway) t=(i%100)/10; Line 25
^ is the xor operator. There is no power operator in C, the is a pow function in the math library. However it uses the double type so will introduce inherent tolerance errors preventing accurate use of the operator == so you may need to write your own version using int/long.

Edited 6 Years Ago by Banfa: n/a

This question has already been answered. Start a new discussion instead.