# Check If Entered Number Is Armstrong Or Not In C#

## Introduction

In this blog, we will create a program in c# to check whether the entered number is an Armstrong number or not.

## What is an Armstrong Number?

An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits. A number that is the sum of its own digits each raised to the power of the number of digits. An Armstrong number of three digits is an integer where the sum of the cubes of its digits is equal to the number itself.

Let's try to understand why 371 is an Armstrong number.
1. 371 = (3*3*3)+(7*7*7)+(1*1*1)
2. where:
3. (3*3*3)=27
4. (7*7*7)=343
5. (1*1*1)=1
6. So:
7. 27+343+1=371
C# Code
1. using System;
2.
3. namespace ArmstrongNumbers
4. {
5.     class Program
6.     {
7.         static void Main(string[] args)
8.         {
9.             int number, rem, temp, sum = 0;
10.             Console.WriteLine("-------------------------------------------------------");
11.             Console.WriteLine("          Check Number Is Armstrong Or Not             ");
12.             Console.WriteLine("-------------------------------------------------------");
13.             Console.Write("Enter Your Number To Check :: ");
15.
16.             temp = number;
17.             while (number > 0)
18.             {
19.                 rem = number % 10;
20.                 sum =sum+(rem*rem*rem);
21.                 number= number/ 10;
22.             }
23.
24.             if (temp == sum)
25.                 Console.WriteLine(temp + " Is A Armstrong Number");
26.             else
27.                 Console.WriteLine(temp + " Is Not A Armstrong Number");
29.         }
30.     }
31. }
Code Explanation
1. First of all, we define four integer variable numbers, sum, rem and temp then assign zero (0) to the sum.
2. Then get user input by Console.ReadLine() and pass this input to the number variable by converting it to an integer.
3. Now assign this user input number to our temp variable for future use.
4. Then we iterate while look until the number became zero(0).
5. In loop:
• First, we find the Remainder of the number by module its by 10.
• Then we get the power of this remainder by simply multiply it three times and add in our sum variable.
• Then assign the result of sum and power of remainder to the sum variable.
• Last, we divide the number by 10.
6. After a loop, check if the temp variable and sum variable are equal. If both are equal, then it's an Armstrong number, otherwise not.
7. Then we use the Console.ReadKey() method to read any key from the console. By entering this line of code, the program will wait and not exit immediately. The program will wait for the user to enter any key before finally exiting. If you don't include this statement in the code, the program will exit as soon as it is run.
Output

I hope you like this blog and find it helpful. If you find it useful kindly share it with friends.