C Program to reverse an array elements


In this program, You will learn how to reverse array elements in c.


List is: 10 20 30 40 50 60

After reverse elements: 60 50 40 30 20 10

Example: How to reverse array elements in c.

#include<stdio.h>

int main() {

   int arr[10], i, j, s, temp;

   printf("Enter size of an array:");
   scanf("%d", &s);

   printf("Enter array elements:");
   for (i = 0; i < s; i++) {
       scanf("%d", &arr[i]);
   }

   j = s - 1;
   for (i = 0; i < j; i++) {
       temp = arr[i];
       arr[i] = arr[j];
       arr[j] = temp;
       j--;
   }
   printf("After reverse array elements:");
   for (i = 0; i < s; i++) {
       printf("%d ", arr[i]);
   }

   return 0;
}

Output:

Enter size of an array:5                                                                                                               
Enter array elements:10 20 30 40 50                                                                                                    
After reverse array elements:50 40 30 20 10

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.