public static void Main(string[] args) { int r, sum = 0, count = 0,rem; Console.WriteLine("Enter a number"); int num = Convert.ToInt32(Console.ReadLine()); int x = num; while (x > 0) { r = x % 10; count++; x = x / 10; } while (num > 0) { rem = num % 10; sum = sum + ((int)Math.Pow(rem, count)); num = num / 10; } Console.WriteLine("Armstrong number = {0}",sum); }
@@monikaphalle9174 Okay, actually you forget about 1 in 153 there is 1 also present, so the cube of 1 is 1,.... Now add the cube of each digit 27+125+1=153
... Easiest way ever .....good sir.
public static void Main(string[] args)
{
int r, sum = 0, count = 0,rem;
Console.WriteLine("Enter a number");
int num = Convert.ToInt32(Console.ReadLine());
int x = num;
while (x > 0)
{
r = x % 10;
count++;
x = x / 10;
}
while (num > 0)
{
rem = num % 10;
sum = sum + ((int)Math.Pow(rem, count));
num = num / 10;
}
Console.WriteLine("Armstrong number = {0}",sum);
}
Good explanation
Thanks
for 153 number we got lastly num value 0 then sum is 1 ,how 1 and 0 equals,this number is armstrong,confused can u explain
Thanks brother
Your welcome
this will only work with 3 digits
Need to multiply the times the length of number eg: if 4 digit number then its sum+(remainder*remainder*remainder*remainder)
When number is 15 then how u calculated sum equal to 125 please explain
get the last digit of 15, it will be 5. Now the cube of 5 is 125. Hope now its clear for you
@@InterviewPoint no but sum is 27 so 27+125 should be 152
@@monikaphalle9174 Okay, actually you forget about 1 in 153 there is 1 also present, so the cube of 1 is 1,....
Now add the cube of each digit 27+125+1=153
@@monikaphalle9174 Please use the debugger and check the value at each point. Because logic is correct.