# C Program to Reverse Elements of an Array

## Reverse elements of an array in C

Here, in this page we will discuss the program to reverse elements of an array in C programming language. We are given with an array and need to reverse the elements. We will discuss various algorithms to reverse the array in efficient way. Here, in page we will discuss various method for reversing the array. Different methods are :

## Method Discussed

• Method 1: Just print the array in reverse order
• Method 2: Actual in-place reversing of the original array
• Method 3: Recursive in-place reversing of the original array

### Example :

• Input : arr = [10, 20, 30, 40, 50]
• Output : Array after reversing, arr = [50, 40, 30, 20, 10]

We will discuss all of these methods one by one below – ## Method 1

For an array: arr

• Run an iterative loop from the last index of the array
• Print each item one by one arr[i]

Let’s see how it works below –

### Method 1 Code in C

Run
```#include <stdio.h>

void printReverse(int arr[], int len){

for(int i = len - 1; i >= 0; i--)
printf("%d ", arr[i]);
}

int main()
{
int arr[] = {10, 20, 30, 40, 50, 60};

int len = sizeof(arr)/sizeof(arr);

printf("Array in Reverse:\n");
printReverse(arr, len);

return 0;
}
```

### Output

```Array in Reverse:60 50 40 30 20 10
```

## Method 2

For an array: arr[] with length len.

• Call a function void reverse(int arr[], int start, int end)
• Intial calling as reverse(arr, 0, len-1);
• Swap items at start & end indexes
• Do start++ & end–
• Stop when start & end indexes overlap one another
• Print the array

#### Time and Space Complexity :

• Time – Complexity : O(n)
• Space-Complexity : O(1) ### Method 2 Code in C

Run
```#include <stdio.h>

void reverse(int arr[], int start, int end)
{
while (start < end)
{
// swap arr[start] & arr[end]
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;

start++;
end--;
}
}

void display(int arr[], int len){

for(int i = 0; i < len; i++)
printf("%d ", arr[i]);
}

int main()
{
int arr[] = {10, 20, 30, 40, 50, 60};

int len = sizeof(arr)/sizeof(arr);

reverse(arr, 0, len-1);

printf("Array in Reverse:\n");
display(arr, len);

return 0;
}
```

### Output

```Array in Reverse:60 50 40 30 20 10
```

## Method 3

In this method, we will use recursion. Call function reverseRecursive(int arr[], int start, int end)

Initially pass values of start = 0, end = len – 1

• Function gets called as reverseRecursive(arr, 0, len-1);
• In each recursive iteration swap arr[start] & arr[end]
• Make further recursive calls as reverseRecursive(arr, start + 1, end – 1);
• return when (start >= end)
• Print the array

#### Time and Space Complexity :

• Time – Complexity : O(n)
• Space-Complexity : O(n), require stack to store the recursive calls. ### Method 3 Code in C

Run
```#include <stdio.h>

void reverseRecursive(int arr[], int start, int end)
{
if (start >= end)
return;

int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;

// recursive call to swap arr[start+1] & arr[end-1]
reverseRecursive(arr, start + 1, end - 1);
}

void display(int arr[], int len){

for(int i = 0; i < len; i++)
printf("%d ", arr[i]);
}

int main()
{
int arr[] = {10, 20, 30, 40, 50, 60};

int len = sizeof(arr)/sizeof(arr);

reverseRecursive(arr, 0, len-1);

printf("Array in Reverse:\n");
display(arr, len);

return 0;
}```

### Output

```Array in Reverse:60 50 40 30 20 10
```

## Important Codes related to Arrays

• Find Smallest Element in an Array : C | C++ | Java | Python
• Find Second Smallest Element in an Array : C | C++ | Java | Python
• Find Largest element in an array : C | C++ | Java | Python
• Find the Smallest and largest element in an array : C | C++ | Java | Python
• Calculate the sum of elements in an array : C | C++ | Java | Python
• Reverse an Array : C | C++ | Java | Python
• Sort first half in ascending order and second half in descending : C | C++ | Java | Python
• Sort the elements of an array : C | C++ | Java | Python
• Finding the frequency of elements in an array : C | C++ | Java | Python
• Sorting elements of an array by frequency : C | C++ | Java | Python
• Finding the Longest Palindrome in an Array : C | C++ | JavaPython
• Counting Distinct Elements in an Array : C | C++ | JavaPython
• Finding  Repeating elements in an Array : C++ | Java | Python
• Finding Non Repeating elements in an Array : C | C++ | Java | Python
• Removing Duplicate elements from an array : C | C++ | Java | Python
• Finding Minimum scalar product of two vectors : C | C++ | Java | Python
• Finding Maximum scalar product of two vectors in an array : C | C++ | Java | Python
• Counting the number of even and odd elements in an array : C | C++ | Java | Python
• Find all Symmetric pairs in an array : C | C++ | Java | Python
• Find maximum product sub-array in a given array : C | C++ | Java | Python
• Finding Arrays are disjoint or not : C | C++ | Java | Python
• Determine Array is a subset of another array or not : C | C++ | Java | Python
• Determine can all numbers of an array be made equal : C | C++ | Java | Python
• Finding Minimum sum of absolute difference of given array : C | C++ | Java | Python
• Sort an array according to the order defined by another array : C | C++ | Java | Python
• Replace each element of the array by its rank in the array : C | C++ | Java | Python
• Finding equilibrium index of an array : C | C++ | JavaPython
• Rotation of elements of array- left and right : C | C++ | JavaPython
• Block swap algorithm for array rotation : C | C++ | JavaPython
• Juggling algorithm for array rotation : C | C++ | Java | Python
• Finding Circular rotation of an array by K positions : C | C++ | Java | Python
• Balanced Parenthesis Problem : C | C++ | Java | Python ### 4 comments on “C Program to Reverse Elements of an Array”

• Shahid

#include

int main()
{
int a={1,2,3,4,5,6,7,8,9,10};
int b;
int i,j,temp;
for(i=0,j=9;i<j;j–,i++){
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
for(i=0;i<10;i++){
printf("%d",a[i]);
}
} 0
• Abhishek

#include
int main(){
int a={1,2,3,4,5};
for (int i = 4; i >= 0; i–)
{
printf(“%d “,a[i]);
}

return 0;
} 8
• Rashmi Sri

#include

int main()
{
int n,a,i;
printf(“enter the sie:”);
scanf(“%d”,&n);
printf(“enter the elements: “);
for(i=0;i=0;i–)
{
printf(“%d “,a[i]);
}
return 0;
} 0
• Rashmi Sri

#include

int main()
{
int n,a,i;
printf(“enter the size:”);
scanf(“%d”,&n);
printf(“enter the elements: “);
for(i=0;i=0;i–)
{
printf(“%d “,a[i]);
}
return 0; 1