Interviewers commonly request programmers to write a program to determine if a given number is an Armstrong number. Let’s delve into implementing Armstrong number in C# today.

Welcome back, visitor. Today, I’ll be implementing a crucial program for those fresh out of secondary school and seeking internships. This program, named “**Armstrong Number**,” employs C# to verify whether a given number qualifies as an Armstrong number. Before we commence, if you haven’t explored our “**Pattern**,” “**Number**,” and “**Pyramid**” tags, do pay them a visit. These tags will aid your comprehension and provide insights into crafting your logic.

## Table of Contents

## What is Armstrong Number

Before delving into any program, it is crucial to determine the logic you intend to implement. Therefore, let’s first grasp the concept of an “Armstrong Number” before proceeding to the next step.

Let’s comprehend Armstrong numbers through an example. Consider the number 370. It comprises 3 digits, correct? Let’s denote them as A (3), B (7), and C (0). Now, let’s initiate the logic. Separate all digits from the given number and multiply each digit by itself, repeated by the total digit count. In our case, since we have 3 digits, the logic would be A * A * A, B * B * B, and C * C * C. Finally, the last step involves summing A + B + C and verifying whether our sum matches the given number.

I have illustrated the steps in the image above, which should help you grasp how the process works.

## Armstrong number in C#

Let’s proceed to the coding section, where I’ll break down each logic into separate sections for easier understanding.

### Step 1

Firstly, we need to determine the total number of digits in the given number. If you’re a regular visitor, you’re likely familiar with the logic for separating each digit from a given number. If not, please visit the “**Check if given number contains a digit in C#**” page, where I’ve described each step in detail on how to extract each digit from a given number.

```
private static int GetTotalDigits(int tempNumber)
{
```**int length = -1; // question for you, why I set default value -1
while (tempNumber > 0)
{
tempNumber /= 10;
length++;
}
return length;**
}

In the first step, there’s a hidden question for all visitors: Why do I set -1 in the length? Comment below if you have the answer.

@interviewspreparation-com

### Step 2

The second step is crucial for all of us; don’t skip this section because I’ll explain it from the beginning.

Now that we have the logic to obtain the total number of digits from the given number, it’s time to implement the logic discussed in the previous section on ‘What is Armstrong Number?‘

First, let’s create the required variables for this logic. In our case, we need to find the total of each digit. Our first variable is an integer named ‘sum’. The second variable is the length of the number, named ‘length’. We obtain this value by calling the ‘GetTotalDigits’ method. Lastly, we have a variable named ‘tempNumber’, which represents our temporary variable to ensure we don’t inadvertently modify the given number.

```
private static bool IsArmstrongNumber(int number)
{
```**int sum = 0;
int length = GetTotalDigits(number);
int tempNumber = number;**
}

Next, let’s apply the same logic as we used in the ‘GetTotalDigits’ method. The logic involves retrieving each digit and multiplying it by the total length of our given number. With each iteration, we add the total of each digit to the ‘sum’ variable. Once the iteration completes, you’ll have a number that follows our ‘What is Armstrong Number?‘ criteria.

```
private static bool IsArmstrongNumber(int number)
{
int sum = 0;
int length = GetTotalDigits(number);
int tempNumber = number;
```** while (number > 0)
{
var reminder = number % 10;**
**
var tempReminder = reminder;
**
** for (int i = 0; i < length; i++)
{
reminder *= tempReminder;
}
**
** sum += reminder;
**
** number /= 10;
}**
**if (sum == tempNumber)
{
return true;
}
else
{
return false;
}**
}

Lastly, we check if the ‘sum’ equals the given number. If they are equal, we return true; otherwise, we return false.

```
static void Main(string[] args)
{
```**int number = 8208;
if (IsArmstrongNumber(number))
{
Console.WriteLine("this is Armstrong number");
}
else
{
Console.WriteLine("this is not Armstrong number");
}**
}
private static bool **IsArmstrongNumber**(int number)
{
int sum = 0;
int length = GetTotalDigits(number);
int tempNumber = number;
while (number > 0)
{
var reminder = number % 10;
var tempReminder = reminder;
for (int i = 0; i < length; i++)
{
reminder *= tempReminder;
}
sum += reminder;
number /= 10;
}
if (sum == tempNumber)
{
return true;
}
else
{
return false;
}
}
private static int **GetTotalDigits**(int tempNumber)
{
int length = -1;
while (tempNumber > 0)
{
tempNumber /= 10;
length++;
}
return length;
}

## Summarizing Armstrong number in C#

Armstrong numbers in C# are determined by a specific logic. Firstly, we find the total number of digits in the given number. Then, we create variables to store the sum of each digit and the length of the number. After that, we iterate through each digit, multiplying it by the total length and adding it to the sum. Finally, we check if the sum equals the given number; if it does, we return true, indicating that the number is an Armstrong number.