6
Reply

Write a program to print all the elements of an array in forward and backward order using single loop. ARRAY is 1,2,3,4,5 output should be 1,2,3,4,5,5,4,3,2,1

Ahmar Husain

Ahmar Husain

10y
3.5k
0
Reply

    for (int i = 0; i < arr.Length*2; i++){if (i > arr.Length-1){Console.Write(arr[(arr.Length*2-i)-1] + "\t");}else{Console.Write(arr[i]+"\t");}}

    int[] arr = { 1, 2, 3, 4, 5 }; for (int i = 0; i < arr.Length * 2; i++) { if (i < arr[arr.Length - 1]) { Console.WriteLine(arr[i]); } else { Console.WriteLine(arr[(arr.Length * 2 - i) - 1]); } }

    int[] arr = { 1, 2, 3, 4, 5 };for (int i = 0; i < arr.Length * 2; i++){if (i < arr[arr.Length - 1]){Console.WriteLine(arr[i]);}else{Console.WriteLine(arr[(arr.Length * 2 - i) - 1]);}}

    int[] array = { 1, 2, 3, 4, 5 };foreach (int value in array){Console.WriteLine(value);}Array.Reverse(array);foreach (int value in array){Console.WriteLine(value);}Console.ReadLine();

    int[] array = { 1, 2, 3, 4, 5 }; foreach (int value in array) { Console.WriteLine(value); } Array.Reverse(array); foreach (int value in array) { Console.WriteLine(value); } Console.ReadLine();

      int[] arr = { 1, 2, 3, 4, 5 };
                for (int i = 0; i < arr.Length * 2; i++)
                {
                    if (i < arr[arr.Length - 1])
                    {
                        Console.WriteLine(arr[i]);
                    }
                    else
                    {
                        Console.WriteLine(arr[(arr.Length * 2 - i) - 1]);
                    }
                }