Do you need to check armstrong number in C#? In this C# tutorial, I will explain to you how to write a program to check Armstrong number in C#.
To determine if a number is an Armstrong number in C#, calculate the sum of its digits each raised to the power of the number of digits. If this sum matches the original number, it’s an Armstrong number. Here’s a snippet:
int sum = 0, temp = number;
while (temp > 0) { sum += (int)Math.Pow(temp % 10, number.ToString().Length); temp /= 10; }
bool isArmstrong = number == sum;
Write a Program to Check Armstrong Number in C#
Let us see, how to write a program in C# to check whether a number is an Armstrong number or not.
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number because 1 power 3+5 power 3 + 3 power 3 = 15313+53+33 = 153. This concept can be extended to n-digit numbers too.
The logic is straightforward: we take a number, split it into its individual digits, raise each digit to the power of the number of digits in the number, sum these powers up, and compare the sum to the original number.
using System;
class ArmstrongNumber
{
static void Main(string[] args)
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int sum = 0, temp = number;
int digits = number.ToString().Length;
while (temp > 0)
{
int digit = temp % 10;
sum += (int)Math.Pow(digit, digits);
temp /= 10;
}
if (sum == number)
Console.WriteLine($"{number} is an Armstrong number.");
else
Console.WriteLine($"{number} is not an Armstrong number.");
}
}
Code:
- Input a Number: We start by taking an integer input from the user.
- Calculate the Number of Digits: We need to know how many digits are there in the number to raise each digit to the power of this count.
- Split and Sum the Powers: We then split the number into its digits, raise each digit to the power of the number of digits, and sum these values.
- Check and Output: Finally, we compare this sum with the original number to determine if it’s an Armstrong number.
You can see here in the below screenshot, after I ran the code using Visual Studio.
Conclusion
This tutorial guides on writing a C# program to check for Armstrong numbers. We’ve discussed what Armstrong numbers are, the logic behind checking for them, and provided a full code example. I hope this tutorial will help you to Write a Program to Check Armstrong Number in C#.
You may also like:
- Write A Program To Find Numbers Above And Below The Average In C#
- Write a C# Program to Print Fibonacci Series
- Write A C# Program To Reverse A String
- How to Swap Two Numbers in C# Without Using a Third Variable?
- Write A C# Program To Check Prime Number
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…