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 Return an Array in C In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C 5 min read C program to reverse the content of the file and print it Given a text file in a directory, the task is to print the file content backward i.e., the last line should get printed first, 2nd last line should be printed second, and so on.Examples: Input: file1.txt has: Welcome to GeeksforGeeks Output: GeeksforGeeks to WelcomeGeeksforGeeks Input: file1.txt has 3 min read Reverse Number Program in C The reverse of a number of means reversing the order of digits of a number. In this article, we will learn how to reverse the digits of a number in C language. Example:Input: 12354Output: 45321Explanation: The number 12354 when reversed gives 45321Input: 623Output: 326Explanation: The number 623 whe 2 min read Program to copy the contents of one array into another in the reverse order Given an array, the task is to copy these array elements into another array in reverse array.Examples: Input: array: 1 2 3 4 5 Output: 5 4 3 2 1 Input: array: 10 20 30 40 50 Output: 50 40 30 20 10 Let len be the length of original array. We copy every element original_arr[i] to copy_arr[n-i-1] to ge 7 min read How to Find the Range of Numbers in an Array in C? The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra 2 min read Like