C# Program to Check Armstrong Number: A Comprehensive Guide with Code Examples

In this article, we will learn about Armstrong numbers and create a C# program to check whether a given number is an Armstrong number or not.

What is an Armstrong Number?

An Armstrong number is a number that is the sum of its own digits, and each is raised to the power of the number of digits. For example, the few Armstrong numbers are 0, 1, 153, 371, 407, 1634, 8208, and 9474.

Let’s take the number 153:

13+53+33

= (1*1*1) + (5*5*5) + (3*3*3)

= 1+125+27

= 153.

In this case, 153 is an Armstrong number because the sum of the cubes of its digits equals the number itself.

Getting Started:

Let’s jump into the code and start understanding and implementing a C# Program to Check Armstrong Number.

Code Example 1: C# Program to Check Armstrong Number

using System;

public class ArmstrongChecker
{
    public static void Main(string[] args)
    {
        int number, sum = 0, originalNumber;

        Console.Write("Enter the Number: ");
        number = int.Parse(Console.ReadLine());

        originalNumber = number;

        while (number > 0)
        {
            int digit = number % 10;
            sum += (int)Math.Pow(digit, 3);
            number /= 10;
        }

        if (originalNumber == sum)
        {
            Console.WriteLine($"{originalNumber} is an Armstrong number.");
        }
        else
        {
            Console.WriteLine($"{originalNumber} is not an Armstrong number.");
        }
    }
}

Output:

Enter the Number: 153
153 is an Armstrong number.
Enter the Number: 127
127 is not an Armstrong number.

C# Code Example 2: Using Function

using System;

class ArmstrongNumberChecker
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());

        int originalNumber = number;
        int result = 0;
        int power = CountDigits(number);

        while (number > 0)
        {
            int digit = number % 10;
            result += (int)Math.Pow(digit, power);
            number /= 10;
        }

        if (result == originalNumber)
        {
            Console.WriteLine($"{originalNumber} is an Armstrong number.");
        }
        else
        {
            Console.WriteLine($"{originalNumber} is not an Armstrong number.");
        }
    }

    static int CountDigits(int num)
    {
        int count = 0;
        while (num > 0)
        {
            num /= 10;
            count++;
        }
        return count;
    }
}

Output:

Enter a number: 371
371 is an Armstrong number.

Code Explanation:

  1. The above C# program asks the user to input a number.
  2. It then uses a helper function named CountDigits to calculate the power needed for Armstrong number checking by counting the digits in the given number.
  3. Summation of Digits: It then iterates through each digit of the number, raises it to the calculated power, and adds the results together.
  4. Comparison: Finally, it compares the computed sum with the original number to determine whether it’s an Armstrong number or not.

Here are a few Armstrong numbers:

  • 1 (1^1 = 1)
  • 153 (1^3 + 5^3 + 3^3 = 153)
  • 407 (4^3 + 0^3 + 7^3 = 407)
  • 1634 (1^4 + 6^4 + 3^4 + 4^4 = 1634)
  • 8208 (8^4 + 2^4 + 0^4 + 8^4 = 8208)
  • 9474 (9^4 + 4^4 + 7^4 + 4^4 = 9474)

These numbers satisfy the Armstrong number criteria, where the sum of each digit raised to the power of the number of digits equals the original number.

Recommended Articles:

Shekh Ali
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments