Home »
.Net »
C# Programs
C# - Armstrong Number Program
Learn: What are the Armstrong numbers, how to check whether a number is Armstrong or not, In this program we are going to implement a program in C#, that will check the Armstrong number?
[Last updated : March 19, 2023]
To understand the program of Armstrong numbers, first we should understand the concept of Armstrong numbers.
What is Armstrong Number?
An Armstrong number is a number which is the sum of, power of each digit by total number of digits.
For example:
153 is an Armstrong number: 13 + 53 +33 = 153
C# program to check given number is Armstrong or not
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
Explanation
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.
C# Basic Programs »