c program to reverse an array
c program to reverse an array :- This program reverses the array elements. For example if a is an array of integers with three elementssuch that
a[0] = 1
a[1] = 2
a[2] = 3
then on reversing the array will be
a[0] = 3
a[1] = 2
a[0] = 1
In the program swapping is done to reverse the array elements. Simple or alternative method to reverse array elements is given in comments below.
Given below is the c code to reverse an array .
#include<stdio.h>
#include<conio.h>
main()
{int n, c, j, temp, a[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]);if( n%2 == 0 )
c = n/2 - 1;
else
c = n/2;
for ( j = 0 ; j < c ; j++ )
{temp = a[j];
a[j] = a[n -j - 1];
a[n-j-1] = temp;
}
printf("Reverse array is\n");for( c = 0 ; c < n ; c++ )
printf("%d\n", a[c]);getch();
return 0;
}