C# Arrays | Arrays in C# | Using arrays in C#

using System;

namespace ClassRoomSamples
{
    class Demo05Array1
    {
        static void Main(String[] anyname)
        {
            //Like "int x[10];" in C language,
            //One dimensional array in C# can be declared like below.
            int[] array = new int[10];

            Console.WriteLine("How many numbers?(Max 10)");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine();

            //If n is 5, It goes 0th,1st,2nd,3rd, and 4th iterations
            for (int i = 0; i < n; i++)
            {
                array[i] = int.Parse(Console.ReadLine());
            }

            Console.WriteLine("Contents of array");
            for (int i = 0; i < n; i++)
            {
                Console.Write("{0}\t",array[i]);
            }
            Console.WriteLine();

            Console.WriteLine("Partial display of array");
            for (int i = 3; i < n; i++)
            {
                Console.Write("{0}\t",array[i]);
            }
            Console.WriteLine();

            Console.WriteLine("Contents of array in Reverse Order");
            for(int i = n-1; i >= 0; i--)
            {
                Console.Write("{0}\t",array[i]);
            }
            Console.WriteLine();

            Console.WriteLine("Contents of array");
            foreach(int x in array)
            {
                Console.Write("{0}\t",x);
            }

            Console.ReadKey();
        }
    }
}


Output

How many numbers? (Max 10)
8

10
20
30
40
50
60
70
80

Contents of array
10      20      30      40      50      60      70      80      

Partial display of an array
40      50      60      70      80      

Contents of an array in Reverse Order
80      70      60      50      40      30      20      10      

Contents of array
10      20      30      40      50      60      70      80      0       0

No comments:

Post a Comment