C Program to Traverse an Array Last Updated : 26 Aug, 2024 Comments Improve Suggest changes Like Article Like Report 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 provides the following ways to traverse the whole array:Table of ContentUsing a LoopUsing RecursionNote: It is compulsory to know the size of the array beforehand to traverse it.1. Using a LoopThe most common and straightforward method to traverse an array is a loop. The idea is to use a loop that runs from 0 to N - 1, where N is the number of elements in the array. We can use any loop of our choice but a for loop is preferred.C Program to Traverse an Array Using Loops C // C Program to Traverse an Array using a for Loop #include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; int N = sizeof(arr) / sizeof(arr[0]); // Traverse and print elements using a for loop printf("Array elements using loop: "); for (int i = 0; i < N; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } OutputArray elements using loop: 1 2 3 4 5 Time Complexity: O(N)Auxiliary Space: O(1)2. Using RecursionRecursion can also be used to traverse an array, although it is less efficient due to function call overhead and memory consumption. For this approach, you need to know how to pass arrays to functions in C.Below is the approach to use recursion to traverse the array:Define a recursive function that takes the array arr and the size of the array N as arguments.This function returns when all the elements are traversed.It calls itself for N - 1 elements.At last, prints the current element.C Program to Traverse an Array Using Recursion C // C Program to Traverse an Array using Recursion #include <stdio.h> // Recursive function to print array elements void traverseArray(int arr[], int N) { // When all the elements are traversed if (N <= 0) { return; } traverseArray(arr, N - 1); printf("%d ", arr[N - 1]); } int main() { int arr[] = {1, 2, 3, 4, 5}; int N = sizeof(arr) / sizeof(arr[0]); // Traverse and print elements using recursion printf("Array elements using recursion: "); traverseArray(arr, N); printf("\n"); return 0; } OutputArray elements using recursion: 1 2 3 4 5 Time Complexity: O(N)Auxiliary Space: O(N), due to the recursive calls Comment More infoAdvertise with us Next Article C Program to Traverse an Array C code_r Follow Improve Article Tags : C Programs C Language School Programming DSA Arrays C-Arrays C Basic Programs +3 More Practice Tags : Arrays Similar Reads C Program to Traverse an Array in Reverse 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 2 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 to sort an array using pointers Given an array of size n, the task is to sort this array using pointers in C. Examples: Input: n = 5, arr[] = {0, 23, 14, 12, 9} Output: {0, 9, 12, 14, 23} Input: n = 3, arr[] = {7, 0, 2} Output: {0, 2, 7} Approach: The array can be fetched with the help of pointers with the pointer variable pointin 2 min read C Program to Copy an Array to Another Array In this article, we will learn how to copy all the elements of one array to another array in C.The simplest method to copy an array is by using the memcpy() function. Let's take a look at an example:C#include <stdio.h> #include <string.h> int main() { int arr1[] = {1, 2, 3, 4, 5}; int 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 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 for Binary Search In this article, we will understand the binary search algorithm and how to implement binary search programs in C. We will see both iterative and recursive approaches and how binary search can reduce the time complexity of the search operation as compared to linear search.Table of ContentWhat is Bina 7 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 C Program to Calculate Average of an Array In this article, we will learn how to calculate the average of all elements of an array using a C program.The simplest method to calculate the average of all elements of an array is by using a loop. Let's take a look at an example:C#include <stdio.h> float getAvg(int arr[], int n) { int sum = 2 min read How to Create an Array of Structs in C? In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of 2 min read Like