Basic Programming C# Examples

C# Program to find Armstrong number for Given Range

The Program in C# Program to find Armstrong number for Given Range is given below:

using System;

namespace ArmstrongNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the lower limit here: ");
            int lower = int.Parse(Console.ReadLine());

            Console.WriteLine("Similarly, enter the upper limit: ");
            int upper = int.Parse(Console.ReadLine());

            Console.WriteLine("Okay, the Armstrong numbers in the given range are: ");
            for (int i = lower; i <= upper; i++)
            {
                if (IsArmstrong(i))
                {
                    Console.WriteLine(i);
                }
            }
            Console.ReadLine();
        }

        static int power(int num, int pow)
        {
            int result = 1;
            for (int i = 0; i < pow; i++)
            {
                result = result * num;
            }
            return result;
        }

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

        static bool IsArmstrong(int num)
        {
            int pow = numberOfDigits(num);
            int originalNum = num;
            int result = 0;
            while (num > 0)
            {
                int remainder = num % 10;
                result = result + power(remainder, pow);
                num = num / 10;
            }

            return result == originalNum;
        }
    }
}

Output:

Hello Codeauri Family, enter the lower limit here:
1

Similarly, enter the upper limit:
1000
Okay, the Armstrong numbers in the given range are:
1
2
3
4
5
6
7
8
9153
370
371
407

Pro-Tips💡

Here are the step by step execution of above program:

  1. The first line using System; includes the System namespace in the program. This namespace includes classes like Console that are used in the program.
  2. The next line defines a namespace ArmstrongNumber that contains the program logic.
  3. The Program class contains the main method, Main, that is the entry point of the program.
  4. In the Main method, the program takes two inputs from the user, the lower and upper limits of the range. These inputs are stored in the lower and upper variables.
  5. The program then prints all the Armstrong numbers within the given range. It does this by looping through the range from the lower limit to the upper limit, and checking if each number is an Armstrong number using the IsArmstrong method. If a number is an Armstrong number, it is printed to the console.
  6. The power method calculates the power of a number and returns the result. It takes two parameters: num and pow.
  7. The numberOfDigits method calculates the number of digits in a given number and returns the count.
  8. The IsArmstrong method checks if a number is an Armstrong number. It takes one parameter, num. It first calculates the number of digits in the number, then calculates the sum of cubes of each digit in the number. If the sum is equal to the original number, then the number is an Armstrong number and the method returns true. Otherwise, it returns false.
  9. The program ends with a call to Console.ReadLine() that waits for the user to press the enter key before closing the console window.

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