Basic Programming C# Examples

C# Program to check If Given number is Armstrong number or not

The Program in C# Program to check If Given number is Armstrong number or not is given below:

using System;

namespace ArmstrongNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number to check for Armstrong number:");
            int number = int.Parse(Console.ReadLine());

            int originalNumber = number;
            int result = 0;
            int digits = 0;

            while (originalNumber != 0)
            {
                originalNumber /= 10;
                digits++;
            }

            originalNumber = number;

            while (originalNumber != 0)
            {
                int remainder = originalNumber % 10;
                result += (int) Math.Pow(remainder, digits);
                originalNumber /= 10;
            }

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

            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family, enter the number to check for Armstrong number:
371
371 is an Armstrong number.

Pro-Tips💡

Here are the step by step execution of above program:

  1. The Main method starts by asking the user to enter a number to check for Armstrong number.
  2. The input is stored in the number variable and the originalNumber variable is initialized with the same value.
  3. The number of digits in the input number is calculated by dividing the originalNumber by 10 until it becomes 0 and counting the number of divisions. This value is stored in the digits variable.
  4. The originalNumber is then used in a while loop to calculate the sum of each digit raised to the power of digits.
  5. If the result is equal to the input number, the number is considered an Armstrong number and the message “is an Armstrong number” is displayed.
  6. If the result is not equal to the input number, the number is not considered an Armstrong number and the message “is not an Armstrong number” is displayed.
  7. The program pauses at the end to wait for the user’s input before exiting.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X