Topic-29:Array Traversal in C Language

Traversing an array in C refers to the process of visiting each element of the array sequentially. This can be done using loops such as the for loop or the while loop. Here’s an example of how to traverse an array in C using a for loop:

#include <stdio.h>

int main() {

    // Define an array

    int arr[5] = {1, 2, 3, 4, 5};

    // Traverse the array using a for loop

    printf(“Elements of the array: “);

    for (int i = 0; i < 5; i++) {

        printf(“%d “, arr[i]);

    }

    printf(“\n”); 

    return 0;

}

 

OUTPUT:

Elements of the array: 1 2 3 4 5

Leave a Comment

Your email address will not be published. Required fields are marked *