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:
- The first line
using System;
includes the System namespace in the program. This namespace includes classes like Console that are used in the program. - The next line defines a namespace
ArmstrongNumber
that contains the program logic. - The
Program
class contains the main method,Main
, that is the entry point of the program. - 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 thelower
andupper
variables. - 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. - The
power
method calculates the power of a number and returns the result. It takes two parameters:num
andpow
. - The
numberOfDigits
method calculates the number of digits in a given number and returns the count. - 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 returnstrue
. Otherwise, it returnsfalse
. - 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.