C program to reverse array: Reverse array in c is a program that reads the given array reverse the given array.

Reverse array in c: This program reverses the given array elements.
Example for reverse array :-
Consider the array values with 4 elements
values[4] = {10, 20, 30, 40}
values [0] = 10 , values[1]= 20, values [2]= 30, values [3]= 40
Then reverse array in c is,
values [0] = 40 , values[1]= 30, values [2]= 20, values [3]= 10

Reverse array algorithm

  1. Declare variable
  2. Take array input. a[]
  3. Traverse array
    For i = end to start
    Copy array to temporary array
    temp[i] = a[i]
  4. Copy temporary array to original array
    a[] = temp[]
  5. Print revered array

C program to reverse array

#include <stdio.h>
int main()

{

int n, c, d, a[100], b[100];
printf("Enter the number of elements in array\n");

scanf("%d", &n);
printf("Enter the array elements\n");
for (c = 0; c < n ; c++)
scanf("%d", &a[c]);

for (c = n - 1, d = 0; c >= 0; c--, d++)

b[d] = a[c];

for (c = 0; c < n; c++)
a[c] = b[c];
printf("Reverse array is\n");
for (c = 0; c < n; c++)
printf("%d\n", a[c]);
return 0;

}

Output :

C program to reverse array

Write A Comment