Write a c program to reverse the elements of a given array
BM Balmiki Mandal 27 Dec 2022 0 views 0 comments C Programming

Write a c program to reverse the elements of a given array

Array Reversal in C: Step-by-Step Guide

To reverse the elements of a given array in C, we can use the following steps:

  1. Declare an integer variable i to iterate over the array.
  2. Initialize i to 0.
  3. While i is less than the length of the array:
    • Swap the elements at indices i and length - i - 1.
    • Increment i by 1.
  4. Return the array.

The following code shows how to implement these steps in C:

C programming
void reverseArray(int arr[], int length) {
  int i;

  for (i = 0; i < length / 2; i++) {
    int temp = arr[i];
    arr[i] = arr[length - i - 1];
    arr[length - i - 1] = temp;
  }
}

int main() {
  int arr[] = {1, 2, 3, 4, 5};
  int length = sizeof(arr) / sizeof(arr[0]);

  reverseArray(arr, length);

  for (int i = 0; i < length; i++) {
    printf("%d ", arr[i]);
  }
  printf("\n");

  return 0;
}

Output:

 

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

Bit wise operators Assignment

Bitwise Operators practical assignment: Take runtime input, bit-level manipulation, and different data units r...

AG Anvesh G 27 Sep 2026
Understanding Float and Double Binary Representation in C

Understanding Float and Double Binary Representation in CIntroduction:When we store decimal numbers such as 10...

AG Anvesh G 31 May 2026
Topic-wise C Programming Interview Questions

C Programming Interview Questions – Topic Wise1. IntroductionC programming is one of the most important langu...

AG Anvesh G 30 May 2026