Reverse array using recursion

Here given code implementation process.

``````//C Program
//Reverse array using recursion
#include <stdio.h>

//print array elements
void display_array(int data[], int size)
{
//Loop which is iterating array elements
for (int i = 0; i < size; ++i)
{
printf("%d ", data[i]);
}
printf("\n");
}
//This is reversing the array elements of given array
void reverse_array(int data[], int front, int tail)
{
if (front < tail)
{
//Interchange of array elements
int auxiliary = data[front];
data[front] = data[tail];
data[tail] = auxiliary;
//Recursive execute
reverse_array(data, front + 1, tail - 1);
}
}
int main()
{
//Define the array elements
int data[] = {
1 , 2 , 7 , 3 , 4 , 5 , 8 , 9
};
//Get the size of array
int size = sizeof(data) / sizeof(data[0]);
printf("Before Reverse \n");
display_array(data, size);
reverse_array(data, 0, size - 1);
printf("After Reverse \n");
display_array(data, size);
return 0;
}``````

Output

``````Before Reverse
1 2 7 3 4 5 8 9
After Reverse
9 8 5 4 3 7 2 1``````
``````// Program for
// Reverse array using recursion in Java
class MyArray
{
//print array elements
public void display_array(int[] data, int size)
{
//Loop which is iterating array elements
for (int i = 0; i < size; ++i)
{
System.out.print("" + data[i] + " ");
}
System.out.print("\n");
}
//This is reversing the array elements of given array
public void reverse_array(int[] data, int front, int tail)
{
if (front < tail)
{
//Interchange of array elements
int auxiliary = data[front];
data[front] = data[tail];
data[tail] = auxiliary;
//Recursive execute
reverse_array(data, front + 1, tail - 1);
}
}
public static void main(String[] args)
{
MyArray obj = new MyArray();
//Define the array elements
int[] data = {
1 , 2 , 7 , 3 , 4 , 5 , 8 , 9
};
//Get the size of array
int size = data.length;
System.out.print("Before Reverse \n");
obj.display_array(data, size);
obj.reverse_array(data, 0, size - 1);
System.out.print("After Reverse \n");
obj.display_array(data, size);
}
}``````

Output

``````Before Reverse
1 2 7 3 4 5 8 9
After Reverse
9 8 5 4 3 7 2 1``````
``````//Include header file
#include <iostream>

using namespace std;
// Program for
// Reverse array using recursion in C++
class MyArray
{
public:
//print array elements
void display_array(int data[], int size)
{
//Loop which is iterating array elements
for (int i = 0; i < size; ++i)
{
cout << "" << data[i] << " ";
}
cout << "\n";
}
//This is reversing the array elements of given array
void reverse_array(int data[], int front, int tail)
{
if (front < tail)
{
//Interchange of array elements
int auxiliary = data[front];
data[front] = data[tail];
data[tail] = auxiliary;
//Recursive execute
this->reverse_array(data, front + 1, tail - 1);
}
}
};
int main()
{
MyArray obj = MyArray();
int data[] = {
1 , 2 , 7 , 3 , 4 , 5 , 8 , 9
};
//Get the size of array
int size = sizeof(data) / sizeof(data[0]);
cout << "Before Reverse \n";
obj.display_array(data, size);
obj.reverse_array(data, 0, size - 1);
cout << "After Reverse \n";
obj.display_array(data, size);
return 0;
}``````

Output

``````Before Reverse
1 2 7 3 4 5 8 9
After Reverse
9 8 5 4 3 7 2 1``````
``````//Include namespace system
using System;
// Program for
// Reverse array using recursion in C#
class MyArray
{
//print array elements
public void display_array(int[] data, int size)
{
//Loop which is iterating array elements
for (int i = 0; i < size; ++i)
{
Console.Write("" + data[i] + " ");
}
Console.Write("\n");
}
//This is reversing the array elements of given array
public void reverse_array(int[] data, int front, int tail)
{
if (front < tail)
{
//Interchange of array elements
int auxiliary = data[front];
data[front] = data[tail];
data[tail] = auxiliary;
//Recursive execute
reverse_array(data, front + 1, tail - 1);
}
}
public static void Main(String[] args)
{
MyArray obj = new MyArray();
int[] data = {
1 , 2 , 7 , 3 , 4 , 5 , 8 , 9
};
//Get the size of array
int size = data.Length;
Console.Write("Before Reverse \n");
obj.display_array(data, size);
obj.reverse_array(data, 0, size - 1);
Console.Write("After Reverse \n");
obj.display_array(data, size);
}
}``````

Output

``````Before Reverse
1 2 7 3 4 5 8 9
After Reverse
9 8 5 4 3 7 2 1``````
``````<?php
// Program for
// Reverse array using recursion in Php
class MyArray
{
//print array elements
public	function display_array( \$data, \$size)
{
//Loop which is iterating array elements
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "". \$data[\$i] ." ";
}
echo "\n";
}
//This is reversing the array elements of given array
public	function reverse_array( & \$data, \$front, \$tail)
{
if (\$front < \$tail)
{
//Interchange of array elements
\$auxiliary = \$data[\$front];
\$data[\$front] = \$data[\$tail];
\$data[\$tail] = \$auxiliary;
//Recursive execute
\$this->reverse_array(\$data, \$front + 1, \$tail - 1);
}
}
}

function main()
{
\$obj = new MyArray();
//Define the array elements
\$data = array(1, 2, 7, 3, 4, 5, 8, 9);
//Get the size of array
\$size = count(\$data);
echo "Before Reverse \n";
\$obj->display_array(\$data, \$size);
\$obj->reverse_array(\$data, 0, \$size - 1);
echo "After Reverse \n";
\$obj->display_array(\$data, \$size);
}
main();``````

Output

``````Before Reverse
1 2 7 3 4 5 8 9
After Reverse
9 8 5 4 3 7 2 1``````
``````// Program for
// Reverse array using recursion in Node Js
class MyArray
{
//print array elements
display_array(data, size)
{
//Loop which is iterating array elements
for (var i = 0; i < size; ++i)
{
process.stdout.write("" + data[i] + " ");
}
process.stdout.write("\n");
}
//This is reversing the array elements of given array
reverse_array(data, front, tail)
{
if (front < tail)
{
//Interchange of array elements
var auxiliary = data[front];
data[front] = data[tail];
data[tail] = auxiliary;
//Recursive execute
this.reverse_array(data, front + 1, tail - 1);
}
}
}

function main()
{
var obj = new MyArray();
//Define the array elements
var data = [1, 2, 7, 3, 4, 5, 8, 9];
//Get the size of array
var size = data.length;
process.stdout.write("Before Reverse \n");
obj.display_array(data, size);
obj.reverse_array(data, 0, size - 1);
process.stdout.write("After Reverse \n");
obj.display_array(data, size);
}
main();``````

Output

``````Before Reverse
1 2 7 3 4 5 8 9
After Reverse
9 8 5 4 3 7 2 1``````
``````#  Program for
#  Reverse array using recursion in Python 3
class MyArray :
# print array elements
def display_array(self, data, size) :
# Loop which is iterating array elements
i = 0
while (i < size) :
print("", data[i] ," ", end = "")
i += 1

print("\n", end = "")

# This is reversing the array elements of given array
def reverse_array(self, data, front, tail) :
if (front < tail) :
# Interchange of array elements
auxiliary = data[front]
data[front] = data[tail]
data[tail] = auxiliary
# Recursive execute
self.reverse_array(data, front + 1, tail - 1)

def main() :
obj = MyArray()
# Define the array elements
data = [1, 2, 7, 3, 4, 5, 8, 9]
# Get the size of array
size = len(data)
print("Before Reverse \n", end = "")
obj.display_array(data, size)
obj.reverse_array(data, 0, size - 1)
print("After Reverse \n", end = "")
obj.display_array(data, size)

if __name__ == "__main__": main()``````

Output

``````Before Reverse
1   2   7   3   4   5   8   9
After Reverse
9   8   5   4   3   7   2   1``````
``````#  Program for
#  Reverse array using recursion in Ruby
class MyArray

# print array elements
def display_array(data, size)

# Loop which is iterating array elements
i = 0
while (i < size)

print("", data[i] ," ")
i += 1
end
print("\n")
end
# This is reversing the array elements of given array
def reverse_array(data, front, tail)

if (front < tail)

# Interchange of array elements
auxiliary = data[front]
data[front] = data[tail]
data[tail] = auxiliary
# Recursive execute
self.reverse_array(data, front + 1, tail - 1)
end
end
end
def main()

obj = MyArray.new()
# Define the array elements
data = [1, 2, 7, 3, 4, 5, 8, 9]
# Get the size of array
size = data.length
print("Before Reverse \n")
obj.display_array(data, size)
obj.reverse_array(data, 0, size - 1)
print("After Reverse \n")
obj.display_array(data, size)
end
main()``````

Output

``````Before Reverse
1 2 7 3 4 5 8 9
After Reverse
9 8 5 4 3 7 2 1
``````
``````// Program for
// Reverse array using recursion in Scala
class MyArray
{
//print array elements
def display_array(data: Array[Int], size: Int): Unit = {
//Loop which is iterating array elements
var i: Int = 0;
while (i < size)
{
print("" + data(i) + " ");
i += 1;
}
print("\n");
}
//This is reversing the array elements of given array
def reverse_array(data: Array[Int], front: Int, tail: Int): Unit = {
if (front < tail)
{
//Interchange of array elements
var auxiliary: Int = data(front);
data(front) = data(tail);
data(tail) = auxiliary;
//Recursive execute
reverse_array(data, front + 1, tail - 1);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyArray = new MyArray();
//Define the array elements
var data: Array[Int] = Array(1, 2, 7, 3, 4, 5, 8, 9);
//Get the size of array
var size: Int = data.length;
print("Before Reverse \n");
obj.display_array(data, size);
obj.reverse_array(data, 0, size - 1);
print("After Reverse \n");
obj.display_array(data, size);
}
}``````

Output

``````Before Reverse
1 2 7 3 4 5 8 9
After Reverse
9 8 5 4 3 7 2 1``````
``````// Program for
// Reverse array using recursion in Swift
class MyArray
{
//print array elements
func display_array(_ data: [Int], _ size: Int)
{
//Loop which is iterating array elements
var i: Int = 0;
while (i < size)
{
print("", data[i] ," ", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
//This is reversing the array elements of given array
func reverse_array(_ data: inout[Int], _ front: Int, _ tail: Int)
{
if (front < tail)
{
//Interchange of array elements
let auxiliary: Int = data[front];
data[front] = data[tail];
data[tail] = auxiliary;
//Recursive execute
self.reverse_array(&data, front + 1, tail - 1);
}
}
}
func main()
{
let obj: MyArray = MyArray();
//Define the array elements
var data: [Int] = [1, 2, 7, 3, 4, 5, 8, 9];
//Get the size of array
let size: Int = data.count;
print("Before Reverse \n", terminator: "");
obj.display_array(data, size);
obj.reverse_array(&data, 0, size - 1);
print("After Reverse \n", terminator: "");
obj.display_array(data, size);
}
main();``````

Output

``````Before Reverse
1   2   7   3   4   5   8   9
After Reverse
9   8   5   4   3   7   2   1``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.