Basic Programming C# Examples

C# Program to Compute Difference Between largest and smallest values in Given array

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:

  1. The first line using System; is a directive that includes the System namespace, which contains several important classes, including the Console class used later in the code.
  2. The next line namespace ArrayDifference declares the namespace for the code, which helps organize it and avoid naming collisions with other code.
  3. The next line class Program declares the main class for the code, which contains the Main method that will be executed when the program is run.
  4. The next line static void Main(string[] args) declares the Main 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.
  5. 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.
  6. The next line int numberOfElements = Convert.ToInt32(Console.ReadLine()); reads the user’s input as a string, converts it to an integer using the Convert.ToInt32 method, and stores the result in the numberOfElements variable.
  7. The next line int[] numbers = new int[numberOfElements]; creates an array of integers with numberOfElements elements, which will be used to store the user’s input.
  8. The next line int min = int.MaxValue; declares and initializes a variable min to the maximum value of an int, which is used as the initial value for finding the minimum value in the array.
  9. The next line int max = int.MinValue; declares and initializes a variable max to the minimum value of an int, which is used as the initial value for finding the maximum value in the array.
  10. The next line for (int i = 0; i < numberOfElements; i++) starts a for loop that will iterate numberOfElements 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.

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