sir,
why this code is not work i is run only for 1
using System;
public class Arm2
{
public static void Main()
{
int reminder, i, num;
for(i=1; i<1000; i++)
{
int sum = 0;
num = i;
while(num>0)
{
reminder = num % 10;
sum = reminder*reminder*reminder;
num = num/10;
}
if(sum == i)
{
Console.WriteLine("{0} is armstrong number.", i);
}
}
Console.ReadLine();
}
}
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

VulpesPosted Mar 15, 2015, 6:20 PM
Technically, an Armstrong number is an N digit number which is the sum of its digits raised to the power N. See for example this link:
http://everything2.net/index.pl?node_id=1407017&displaytype=printable&lastnode_id=1407017
So:
for one digit numbers, you raise to the power 1.
for two digit numbers, you raise to the power 2.
for three digit numbers, you raise to the power 3.
In other words, you only cube the digits if it's a 3 digit number.
Allowing for this, the program should now read:
and the output is:
Tom MohanPosted Mar 15, 2015, 3:48 AM
sum = reminder*reminder*reminder;
use
sum += reminder * reminder * reminder;
pleas see the changes.
public static void Main()
{
int reminder, i, num;
for (i = 1; i < 1000; i++)
{
int sum = 0;
num = i;
while (num > 0)
{
reminder = num % 10;
sum += reminder * reminder * reminder;
num = num / 10;
}
if (sum == i)
{
Console.WriteLine("{0} is armstrong number.", i);
}
}
Console.ReadLine();
}
}
Mark it as "Accepted Answer", If its useful