armstrong number

A

ashu

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;
}
 
R

Richard Heathfield

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;
}
 
T

Thad Smith

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top