using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = 0;
int digiCount = 0;
int number = 0;
int tNumber = 0;
int []digiArray = new int[10];
double sum = 0;
Console.Write("Enter Number : ");
tNumber = number = int.Parse(Console.ReadLine());
//To find out total number of digits in number
while (number > 0)
{
digiArray[i++] = number %10;
number = number / 10;
digiCount++;
}
for(i=0;i<digiCount;i++)
{
sum += Math.Pow(digiArray[i], digiCount);
}
if (sum == tNumber)
Console.WriteLine("Given Number is armstrong");
else
Console.WriteLine("Given Number is not armstrong");
}
}
}
Output
Enter Number : 153
Given Number is armstrong
In above program, first of all we are finding total number of digits in given number, and store each digit into an array then using power method of Math class, find power then calculate sum of each result, then comparing sum and number, if it is equal then it is Armstrong otherwise it is not Armstrong.
Consider the program:
Output
In above program, first of all we are finding total number of digits in given number, and store each digit into an array then using power method of Math class, find power then calculate sum of each result, then comparing sum and number, if it is equal then it is Armstrong otherwise it is not Armstrong.