Armstrong number
static void Main(string[] args)
{
Console.Write("Enter a number : ");
int num = int.Parse(Console.ReadLine());
CheckArmstrong(num);
}
static void CheckArmstrong(int num)
{
int sum = 0;
int orig = num;
int length = num.ToString().Length;
for (int i = 1; i <= length; i++)
{
if (num.ToString().Length == 1)
{
sum += Mul(num, length);
}
else
{
sum += Mul(num % 10, length);
}
num /= 10;
}
if (orig == sum)
{
Console.WriteLine("{0} is armstrong number", orig);
}
else
{
Console.WriteLine("{0} is not armstrong number", orig);
}
}
static int Mul(int num, int length)
{
int mul = 1;
for (int i = 1; i <= length; i++)
{
mul *= num;
}
return mul;
}
Comments
Post a Comment