The Program in C# Program to find Strong Numbers within Range of numbers is given below:
using System;
namespace Strong_Numbers
{
class Program
{
static void Main(string[] args)
{
int start, end, i, j, temp, rem, sum;
Console.WriteLine("Hello Codeauri Family,enter the starting number of the range here: ");
start = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Similarly, enter the ending number of the range: ");
end = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Okay, the Strong Numbers in the given range are: ");
for (i = start; i <= end; i++)
{
temp = i;
sum = 0;
while (temp > 0)
{
rem = temp % 10;
sum = sum + Factorial(rem);
temp = temp / 10;
}
if (sum == i)
{
Console.WriteLine(i + " ");
}
}
Console.ReadLine();
}
static int Factorial(int n)
{
int i, fact = 1;
for (i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}
}
}
Output:
Hello Codeauri Family,enter the starting number of the range here:
1
Similarly, enter the ending number of the range:
50
Okay, the Strong Numbers in the given range are:
1
2
Pro-Tips💡
Here are the step by step execution of above program:
using System;
– This line is a using directive, which tells the compiler to use the System
namespace, which includes classes and methods used in the program.
namespace Strong_Numbers
– The namespace declaration creates a namespace named Strong_Numbers
to group classes and other code elements in a logical and organized manner.
class Program
– This is a class declaration, creating a class named Program
.
static void Main(string[] args)
– This is the main method of the program, the entry point for the program. When the program is executed, the code inside the Main
method is executed first.
int start, end, i, j, temp, rem, sum;
– This line declares seven integer variables: start
, end
, i
, j
, temp
, rem
, and sum
.
Console.WriteLine("Enter the starting number of the range: ");
– This line writes a message to the console asking the user to enter the starting number of the range.
start = Convert.ToInt32(Console.ReadLine());
– This line reads the starting number entered by the user, converts it to an integer using the Convert.ToInt32
method, and assigns it to the start
variable.
Console.WriteLine("Enter the ending number of the range: ");
– This line writes a message to the console asking the user to enter the ending number of the range.
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.