c-sharp
  1. c-sharp-program-to-check-armstrong-number

Program to Check Armstrong Number - (C# Basic Programs)

An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits in the number itself. In this tutorial, we will discuss a program to check whether a given number is an Armstrong number or not using C# programming language.

Syntax

The syntax for checking whether a number is an Armstrong number or not in C# is as follows:

int num, originalNum, remainder, result = 0, n = 0;
originalNum = num;

// Find the number of digits
while (originalNum != 0)
{
    originalNum /= 10;
    ++n;
}

originalNum = num;

// Calculate the result
while (originalNum != 0)
{
    remainder = originalNum % 10;
    result += (int)Math.Pow(remainder, n);
    originalNum /= 10;
}

// Check if the number is an Armstrong number
if (result == num)
{
    Console.WriteLine($"{num} is an Armstrong number");
}
else
{
    Console.WriteLine($"{num} is not an Armstrong number");
}

Example

using System;

namespace ArmstrongNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, originalNum, remainder, result = 0, n = 0;
            Console.WriteLine("Enter a positive integer: ");
            num = int.Parse(Console.ReadLine());
            originalNum = num;

            // Find the number of digits
            while (originalNum != 0)
            {
                originalNum /= 10;
                ++n;
            }

            originalNum = num;

            // Calculate the result
            while (originalNum != 0)
            {
                remainder = originalNum % 10;
                result += (int)Math.Pow(remainder, n);
                originalNum /= 10;
            }

            // Check if the number is an Armstrong number
            if (result == num)
            {
                Console.WriteLine($"{num} is an Armstrong number");
            }
            else
            {
                Console.WriteLine($"{num} is not an Armstrong number");
            }
        }
    }
}

Output:

Enter a positive integer: 153
153 is an Armstrong number

Explanation

The program takes an input number from the user and checks whether it is an Armstrong number or not. First, it calculates the number of digits in the entered number. Then, it calculates the result by adding each digit raised to the power of the number of digits. Finally, it compares the calculated result with the original number to check if it is an Armstrong number or not.

Use

The program to check Armstrong number in C# is a great exercise for beginners who are just starting with C# programming. It helps programmers develop their problem-solving skills and understand the concepts of loops, conditional statements, and mathematical calculations.

Summary

In this tutorial, we discussed a program to check whether a given number is an Armstrong number or not using C# programming language. We covered the syntax, example, explanation, use, and benefits of programming to check Armstrong number in C#. By practicing these exercises, programmers can improve their coding skills and become more proficient in solving mathematical problems using programming.

Published on: