The Program in C# Program to Compute Difference Between largest and smallest values in Given array is given below:
using System;
namespace ArrayDifference
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter the number of elements in the array to find the difference between largest and smallest value:");
int numberOfElements = Convert.ToInt32(Console.ReadLine());
int[] numbers = new int[numberOfElements];
int min = int.MaxValue;
int max = int.MinValue;
for (int i = 0; i < numberOfElements; i++)
{
Console.WriteLine("Well, enter an element here" + (i + 1) + ":");
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
foreach (int number in numbers)
{
if (number < min)
{
min = number;
}
if (number > max)
{
max = number;
}
}
Console.WriteLine("Okay, the difference between the largest and smallest values is: " + (max - min));
}
}
}
Output:
Hello Codeauri Family,enter the number of elements in the array to find the difference between largest and smallest value:
3
Well, enter an element here1:
20
Well, enter an element here2:
30
Well, enter an element here3:
10
Okay, the difference between the largest and smallest values is: 20
Pro-Tips💡
Here are the step by step execution of above program:
- The first line
using System;
is a directive that includes theSystem
namespace, which contains several important classes, including theConsole
class used later in the code. - The next line
namespace ArrayDifference
declares the namespace for the code, which helps organize it and avoid naming collisions with other code. - The next line
class Program
declares the main class for the code, which contains theMain
method that will be executed when the program is run. - The next line
static void Main(string[] args)
declares theMain
method, which is the entry point for the program. This method takes an array of strings as an argument, which can be used to pass command-line arguments to the program. - The next line
Console.WriteLine("Enter the number of elements in the array:");
outputs a prompt to the console asking the user to enter the number of elements in the array. - The next line
int numberOfElements = Convert.ToInt32(Console.ReadLine());
reads the user’s input as a string, converts it to an integer using theConvert.ToInt32
method, and stores the result in thenumberOfElements
variable. - The next line
int[] numbers = new int[numberOfElements];
creates an array of integers withnumberOfElements
elements, which will be used to store the user’s input. - The next line
int min = int.MaxValue;
declares and initializes a variablemin
to the maximum value of anint
, which is used as the initial value for finding the minimum value in the array. - The next line
int max = int.MinValue;
declares and initializes a variablemax
to the minimum value of anint
, which is used as the initial value for finding the maximum value in the array. - The next line
for (int i = 0; i < numberOfElements; i++)
starts afor
loop that will iteratenumberOfElements
times.
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.