C Program to Traverse an Array in Reverse Last Updated : 26 Aug, 2024 Comments Improve Suggest changes Like Article Like Report Write a C program to traverse a given array in reverse order that contains N elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3}Output: -3 0 6 5 -1 2Input: arr[] = {4, 0, -2, -9, -7, 1}Output: 1 -7 -9 -2 0 4Different Ways to Traverse an Array in Reverse Order in CWe can traverse/print the array in the reverse direction using the following different methods in C:1. Using a LoopThe most straightforward method to traverse an array in reverse is by using a loop. This involves iterating from the last index (N - 1) to the first index (0).C Program to Traverse an Array in Reverse Order Using a Loop C // C Program to Traverse an Array in Reverse Order Using a Loop #include <stdio.h> int main() { int arr[] = {2, -1, 5, 6, 0, -3}; int N = sizeof(arr) / sizeof(arr[0]); // Loop that goes from N - 1 to 0 for (int i = N - 1; i >= 0; i--) { printf("%d ", arr[i]); } return 0; } Output-3 0 6 5 -1 2 Time Complexity: O(N)Auxiliary Space: O(1)2. Using RecursionRecursion can also be used to traverse an array in reverse order. It is less efficient than looping due to the overhead of function calls and additional memory usage.Below is the approach to use recursion to traverse the array in revese:Define a recursive function that takes the array and the current index as arguments.The base case is when all elements are traversed.Recursively call the function for the next element until the first is reached.Print the current element after the recursive call.C Program to Traverse an Array in Reverse Order Using Recursion C // C Program to Traverse an Array in Reverse Order Using Recursion #include <stdio.h> void traverseReverseRecursive(int arr[], int N) { if (N <= 0) { return; } // Print the current element after recursive call printf("%d ", arr[N - 1]); traverseReverseRecursive(arr, N - 1); } int main() { int arr[] = {2, -1, 5, 6, 0, -3}; int N = sizeof(arr) / sizeof(arr[0]); // Traverse and print the array in reverse order using recursion traverseReverseRecursive(arr, N); return 0; } Output-3 0 6 5 -1 2 Time Complexity: O(N)Auxiliary Space: O(N), due to the recursive stack usage. Comment More infoAdvertise with us Next Article C Program to Traverse an Array in Reverse A abhishekcpp Follow Improve Article Tags : C Programs C Language DSA C-Arrays C Basic Programs +1 More Similar Reads C Program to Traverse an Array Write a C program to traverse the given array that contains N number of elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1Different Ways to Traverse an Array in CArrays are versatile data structures and C language pro 3 min read C Program to Traverse a Multi-Dimensional Array Write a C program to traverse a given multi-dimensional array that contains N elements.ExamplesInput: arr[2][3] = {{1, 2, 3}, {4, 5, 6}}Output: 1 2 3 4 5 6Input: arr[3][2] = {{-1, -2}, {0, 3}, {5, 7}}Output: -1 -2 0 3 5 7Different Ways to Traverse a Multi-Dimensional Array in CThe most common method 3 min read C Program for Reversal algorithm for array rotation Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements. Example: Input: arr[] = [1, 2, 3, 4, 5, 6, 7] d = 2 Output: arr[] = [3, 4, 5, 6, 7, 1, 2] Rotation of the above array by 2 will make array Algorithm : rotate(arr[], d, n) reverse(arr[], 1, d) ; reverse(arr[], d + 1, n); 3 min read Reverse Array in C Reversing an array means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse an array in C.The simplest method to reverse an array in C program is by using two pointers: one starting at the beginning (left) and 3 min read C Program to Sort an Array in Ascending Order Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element.The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:C 3 min read Like