C#

# Programmingempire

This article contains Programs to Find Armstrong Numbers in C#. To begin with, let us first understand what is an Armstrong number?

### Armstrong Number

To illustrate, suppose we have a number n. Further, suppose it has total m digits. Now take each digit of the number at a time and compute the value of that digit raised to the power m. After that find sum of all such computed values. Basically, if the resulting sum is equal to the number n, then it is an Armstrong number.

For example,

Let n=1634. Therefore, m=4. After that, we compute the following values.

14 = 1

64 = 1296

34 = 81

44 = 256

Further find the sum = 1 + 1296 + 81 +256 = 1634. Since, the resulting sum is equal to the number n =1634, it is an Armstrong number.

### Examples of Armstrong Numbers

The following are some of the examples of Armstrong Numbers.

### Following are Some of the Programs to Find Armstrong Numbers in C#

Basically, One-Digit Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9. Since each digit raised to power 1 is the same as the digit itself. However, there is no two-digit Armstrong number exists. Similarly, three-dist Armstrong numbers are 153, 370v371, 407. Likewise, four-digit Armstrong numbers are 1634, 8208, and 9474. Besides, five-digit Armstrong numbers are 54748, 92727, 93084.

The following program shows two functions to find Armstrong numbers. While the function FindArmstrong determines whether a randomly generated number is an Armstrong number or not. Another function called as ArmstrongNumbersInRange checks the range of numbers from 10 to 999 and determines whether any number in this range is an Armstrong number. However, you can also change this range to find more Armstrong numbers.

``````using System;

namespace ArmstrongNumberDemo
{
class Program
{
static void Main(string[] args)
{
bool b2;
Random r = new Random();
int n= r.Next(10, 999);
Console.WriteLine("Checking a randomly generated number...");
b2 = ArmstrongClass.FindArmstrong(n);
if (b2)
Console.WriteLine(n + " is an Armstrong Number.");
else
Console.WriteLine(n + " is not an Armstrong Number.");
Console.WriteLine();
Console.WriteLine("All Armstrong Numbers in the Range [10 - 999]: \n");
ArmstrongClass.ArmstrongNumbersInRange();
}
}
class ArmstrongClass
{
public static bool FindArmstrong(int n)
{
int sum = 0;
int n1 = n;
int remainder;
int result = 0;
while (n != 0)
{
String str = n.ToString();
int strlen = str.Length;
n1 = n;
while(n1!=0)
{
remainder = n1 % 10;
result += (int)Math.Pow(remainder, strlen);

sum += remainder;
n = n / 10;
}
if (n1 == result)
return true;
else
return false;
}
public static void ArmstrongNumbersInRange()
{
int n1, sum = 0;
int remainder;
int result = 0;
for(int n=10;n<=999;n++)
{
n1 = n;
while(n1!=0)
{
remainder = n1 % 10;
result += remainder * remainder * remainder;

sum += remainder;
n1 = n1 / 10;
}
if(n==result)
{
Console.Write(n + " ");
}
result = 0;
sum = 0;
}
}
}
}
``````

Output

Selection Sort in C#

Insertion Sort in C#

Bubble Sort in C#

How to Create Instance Variables and Class Variables in Python

Comparing Rows of Two Tables with ADO.NET

Example of Label and Textbox Control in ASP.NET

One Dimensional and Two Dimensuonal Indexers in C#

Private and Static Constructors in C#

Methods of Array Class

Anonymous Functions in C#

Programs to Find Armstrong Numbers in C#

Matrix Multiplication in C#

One Dimensional and Two Dimensional Indexers in C#

Static Class Example in C#

Rotating an Array in C#

Generic IList Interface and its Implementation in C#

Recursive Binary search in C#

C# Practice Questions

Creating Navigation Window Application Using WPF in C#

Find Intersection Using Arrays

An array of Objects and Object Initializer

Performing Set Operations in LINQ

Using Quantifiers in LINQ

Data Binding Using BulletedList Control

Grouping Queries in LINQ

Generic Binary Search in C#

Understanding the Quantifiers in LINQ

Join Operation using LINQ

Deferred Query Execution and Immediate Query Execution in LINQ

Examples of Query Operations using LINQ in C#

An array of Objects and Object Initializer

Language-Integrated Query (LINQ) in C#

How Data Binding Works in WPF

Examples of Connected and Disconnected Approach in ADO.NET

New Features in C# 9

IEnumerable and IEnumerator Interfaces

KeyValuePair and its Applications

C# Root Class – Object

Access Modifiers in C#

Learning Properties in C#

Learning All Class Members in C#

Examples of Extension Methods in C#

How to Setup a Connection with SQL Server Database in Visual Studio

Understanding the Concept of Nested Classes in C#

LINQ To SQL Examples

A Beginner’s Tutorial on WPF in C#

Explaining C# Records with Examples

Everything about Tuples in C# and When to Use?

Creating Jagged Arrays in C#

Linear Search and Binary search in C#

Learning Indexers in C#

Object Initializers in C#

Examples of Static Constructors in C#

When should We Use Private Constructors?

C# Basic Examples

IEqualityComparer Interface

C#

C#

C#