Ahmar Husain
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
By Ahmar Husain in C# on Nov 25 2013
  • ashish singh
    Oct, 2020 1

    int[] arr = { 1, 2, 3, 4, 5 };
    string a=””;
    string b=””;
    for (int i = 0; i < arr.Length; i++)
    {

    1. a=a+arr[i].ToString();
    2. b=b+arr[arr.Length-i-1].ToString();
    3. }
    4. Console.WriteLine(a);
    5. Console.WriteLine(b);

    • 0
  • vipin kv
    Oct, 2015 6

    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");}}

    • 0
  • Rahul Prajapat
    Jun, 2015 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]); } }

    • 0
  • Pankaj  Kumar Choudhary
    Jun, 2015 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]);}}

    • 0
  • sivaragavi vijaykrishna
    Dec, 2013 18

    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();

    • 0
  • sivaragavi vijaykrishna
    Dec, 2013 18

    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();

    • 0
  • Ahmar Husain
    Nov, 2013 25

    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]);
                    }
                }

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS