469,602 Members | 1,774 Online

# armstrong number

an armstrong number of three digits is an integer such that the sum of
the cubes of its digits is equal to the number itself. for example,
171 is an armstrong number since 3**3+7**7+1**1=371.
i have try but not succed.kindly help me .
here is what i did :

int main()
{
int a,b,n,s=0;
printf("enter no");
scanf("%d",&n);
b=s;
while(n>10)
{
a=n%10;
s=s+(a*a*a);
n=n/10;
}
s=s+(n*n*n);
if(b==s)
printf("armstrong number\n");
return 0;
}

May 14 '07 #1
2 12340 ashu said:
an armstrong number of three digits is an integer such that the sum of
the cubes of its digits is equal to the number itself. for example,
171 is an armstrong number since 3**3+7**7+1**1=371.
More generally, an Armstrong number is a number which, expressed in base
b, has n digits such that the sum of its (base b) digits, each raised
to the power n, is equal to the number itself. Below, we confine
ourselves to three base ten digits, for simplicity.

171 is not an Armstrong number because the cubes of 1, 7, and 1 are 1,
343, and 1 respectively. Their sum is 345. 371 is, however, an
Armstrong number, but this has nothing to do with raising 7 to the
power 7 or 1 to the power 1.
i have try but not succed.kindly help me .
here is what i did :

int main()
{
int a,b,n,s=0;
printf("enter no");
Calling a function without a valid function prototype in scope invokes
undefined behaviour. You forgot to #include <stdio.h>

fflush(stdout) if you want your prompt to appear before you block for
input.
scanf("%d",&n);
scanf returns an important value. Check that it's the right value. If it
isn't, you know something went wrong.
b=s;
while(n>10)
{
a=n%10;
s=s+(a*a*a);
n=n/10;
}
s=s+(n*n*n);
if(b==s)
printf("armstrong number\n");
return 0;
}
int is_armstrong3(int n)
{
int is = 0;
if(n 99 && n < 1000)
{
int h = n / 100;
int t = (n / 10) % 10;
int u = n % 10;
if(h * h * h + t * t * t + u * u * u == n)
{
is = 1;
}
}
return is;
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 14 '07 #2
ashu wrote:
an armstrong number of three digits is an integer such that the sum of
the cubes of its digits is equal to the number itself. for example,
171 is an armstrong number since 3**3+7**7+1**1=371.
See Richard's comment on the example.
i have try but not succed.kindly help me .
here is what i did :

int main()
{
int a,b,n,s=0;
printf("enter no");
scanf("%d",&n);
b=s;
while(n>10)
{
a=n%10;
s=s+(a*a*a);
n=n/10;
}
s=s+(n*n*n);
if(b==s)
printf("armstrong number\n");
What values are being compared and why? That should point you to your
problem.

Also, if the power and the number of digits is required to be the same
by definition of an Armstrong number, then the test may fail for numbers
with the wrong number of digits.

--
 8 posts views Thread by EAS | last post: by 6 posts views Thread by Milo Woodward | last post: by 11 posts views Thread by don | last post: by 1 post views Thread by ipscec | last post: by 4 posts views Thread by himanshu srivastava | last post: by 11 posts views Thread by sriparna mukhopadhyay | last post: by 14 posts views Thread by =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post: by 1 post views Thread by siddhantpratap | last post: by reply views Thread by suresh191 | last post: by reply views Thread by goatbishop | last post: by reply views Thread by Trystan | last post: by 1 post views Thread by strativab | last post: by reply views Thread by PitrL | last post: by 1 post views Thread by tbutler | last post: by 1 post views Thread by Samuh | last post: by reply views Thread by billypeterson | last post: by 4 posts views Thread by guiromero | last post: by