Arrays in C# with Real-Time Examples


Arrays in c# with examples


The array is a user-defined reference type Data type. Which is used to store the multiple values of the same data type in a single variable.

The array is used to store the homogeneous values which means multiple values of the same datatype

There different types of arrays are available. Below is the list

    1. Single Dimension Array
    2. Multi Dimension Array
    3. Jagged Array

Single Dimension Array

It is the simplest data type in c# used to store only a single row to store the data.

Syntax:

Datatype[] arrayname= new  Datatype[Size];

Below is the list of examples on the array.

    1. Simple Array iteration of the integer Array
    2. Sum of the given Integer Array
    3. Simple Array iteration using read the size and entering the custom values
    4. Finding the Largest Number in the given Array Integer
    5. Finding the least Number in the given Array Integer
    6. Swap two numbers without using the third variable
    7. Swap the twp numbers using third variables
    8. Find the sum of even numbers and odd numbers in the given Array
    9. Find the count of even numbers and odd numbers in the given Array
    10. Find the First Largest Number and Second Largest Number in the Given Array
    11. Find the First Lowest Number and Second Lowest Number in the Given Array
    12. Program to swap the Minimum Value and Maximum Value in the given Array


 Simple Array iteration of the integer Array :


using System;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arr = new int[5] {10,20,50,90,100 };

            for (int i=0;i<=arr.Length-1;i++)

            {

                Console.WriteLine(arr[i]);               

            }

            Console.ReadLine();

         }

    }

}             


Sum of the given Integer Array :


using System;

using System.Collections.Generic;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arr = new int[5] { 10,20,30,40,50};

            int sum = 0;

            Console.WriteLine("before sum of the array --> sum= "+sum);

            for (int i=0;i<=arr.Length-1;i++) {

                sum = sum + arr[i];

            }

          Console.WriteLine("sume of the given integer Array --> sum = "+ sum);

           Console.Read();

        }

    }

}                 


Simple Array iteration using read the size and entering the custom values:


using System;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Enter Array Size");

            int size=int.Parse(Console.ReadLine());

            int[] arr= new int[size];

            for (int i=0;i<=size-1;i++)

            {

                Console.WriteLine("Enter Values");

                arr[i] = int.Parse(Console.ReadLine());

            }

            for (int i=0;i<=size-1;i++) {

                Console.WriteLine(arr[i]);

            }

            Console.ReadLine();

        }

    }

}                 


Finding the Largest Number in the given Array Integer :

 

using System;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Enter Array Size Value");

            int size = int.Parse(Console.ReadLine());

           int[] arr = new int[size];

            int max = 0;

 

            for (int i=0;i<=size-1;i++)

            {

                Console.WriteLine("Enter Value");

                arr[i] = int.Parse(Console.ReadLine());

                if (arr[i]>max)

                {

                    max = arr[i];

                }

             }

            Console.WriteLine("Largest Number in the given Array__ "+max);

            Console.ReadLine();  

        }

    }

}



Finding the least Number in the given Array Integer :


using System;

using System.Collections.Generic;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Enter Array Size");

            int size = int.Parse(Console.ReadLine());

            int[] arr = new int[size];

            int min = 0;

            Console.WriteLine("Enter Number");

            for (int i=0;i<=size-1;i++)

            {

                arr[i] = int.Parse(Console.ReadLine());

            }

            for (int i=0;i<=size-1;i++)

            {

                if (arr[i]<min)

                {

                    min = arr[i];

                }

            }

            Console.WriteLine("the lasgest Numer is "+min);

            Console.ReadLine();

        }

    }

}



Swap two numbers without using the third variable :


using System;

using System.Collections.Generic;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            int a = 9999;

            int b = 1111;

            Console.WriteLine("swap the 2 numbers without using third variable");

            Console.WriteLine("the given 2 variables are a= "+a+" and b= "+b);

            a = a + b;

            b = a - b;

            a = a - b;

            Console.WriteLine("after swap the values are a= "+a+" and b= "+b);

            Console.ReadLine();

        }

    }

}


Swap the twp numbers using third variables :


using System.Collections.Generic;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            int a = 9999;

            int b = 1111;

            int c = 0;

            Console.WriteLine("swap the 2 numbers without using third variable");

            Console.WriteLine("the given 2 variables are a= "+a+" and b= "+b);

            c = a + b;

            a = c - a;

            b = c - b;

            Console.WriteLine("after swap the values are a= "+a+" and b= "+b);

            Console.ReadLine();

        }

    }

}



Find the sum of even numbers and odd numbers in the given Array


namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arr = new int[5] { 1,3,6,7,4};

            int evenSum = 0;

            int oddSum = 0;

          

            for (int i=0;i<=arr.Length-1;i++) {

                if (arr[i]%2==0)

                {

                    evenSum = evenSum + arr[i];

                }

                else

                {

                    oddSum = oddSum + arr[i];

                }

              

            }

                 Console.WriteLine("even sum = "+ evenSum);

                 Console.WriteLine("Odd sum = " + oddSum);

            Console.Read();

        }

    }

}                             


Find the count of even numbers and odd numbers in the given Array


     using System;

using System.Collections.Generic;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arr = new int[5] { 1,3,6,7,4};

            int evenCount = 0;

            int oddCount = 0;

          

            for (int i=0;i<=arr.Length-1;i++) {

                if (arr[i]%2==0)

                {

                    evenCount++;

                }

                else

                {

                    oddCount++;

                }

              

            }

            Console.WriteLine("even Count = " + evenCount);

            Console.WriteLine("odd Count = " + oddCount);

            Console.Read();

        }

    }

}



Find the First Largest Number and Second Largest Number in the Given Array :



using System.Collections.Generic;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arr = new int[5] { 1,3,6,7,4};

            int firstLargestNumber = 0;

            int secondLargestNumber = 0;

 

            for (int i = 0; i <= arr.Length - 1; i++)

            {

                if (arr[i]>firstLargestNumber)

                {

                    firstLargestNumber = arr[i];

                }

              

            }

            for (int i = 0; i <= arr.Length - 1; i++)

            {    

              if (arr[i] > secondLargestNumber && firstLargestNumber != arr[i])

                {

                    secondLargestNumber = arr[i];

                }

            }

            Console.WriteLine("First Largest Number = "+ firstLargestNumber);

            Console.WriteLine("Second Largest Number = " + secondLargestNumber);

            Console.ReadLine();

        }

    }

}     


Find the First Lowest Number and Second Lowest Number in the Given Array :


using System.Collections.Generic;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arr = new int[7] { 1,3,6,7,4,2,9 };

            int firstLowestNumber = 9;

            int secondLowestNumber = 9;

 

            for (int i = 0; i <= arr.Length - 1; i++)

            {

                if (arr[i]< firstLowestNumber)

                {

                    firstLowestNumber = arr[i];

                }

            }

            for (int i = 0; i <= arr.Length - 1; i++)

            {              

                if (arr[i] < secondLowestNumber && firstLowestNumber != arr[i])

                {

                    secondLowestNumber = arr[i];

                }

            }

            Console.WriteLine("First Largest Number = "+ firstLowestNumber);

            Console.WriteLine("Second Largest Number = " + secondLowestNumber);

            Console.ReadLine();

        }

    }

}              


Program to swap the Minimum Value and Maximum Value in the given Array :


using System;

using System.Collections.Generic;

namespace ConsolePractice

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arr = new int[6] { 0,1,2,3,4,5 };

            for (int i=0; i <= arr.Length - 1;i++)

            {

                Console.Write(" "+arr[i]);

            }

            Console.WriteLine("-----");

            int min = 0;

            int max = 0;

            int minIndex = 0;

            int maxIndex = 0;

            for (int i=0;i<=arr.Length-1;i++)

            {

                if (max<arr[i])

                {

                    max = arr[i];

                    maxIndex = i;

                }

            }

            for (int i=0;i<arr.Length-1;i++)

            {

                if (min>arr[i])

                {

                    min = arr[i];

                    minIndex = i;

                }

            }

            arr[maxIndex] = min;

            arr[minIndex] = max;

            for (int i=0;i<arr.Length;i++)

            {

               

                Console.Write(" "+arr[i]);

            }

 

            Console.ReadLine();

        }

    }

}              





Previous Post Next Post