C Program to Reverse Array
Question: Write a program in C to read an array containing n numbers and reverse this array.
Program
#include<stdio.h>
int main()
{
int a[100], temp, i, n;
printf("Enter n:");
scanf("%d", &n);
/* Reading Array */
printf("Enter numbers:\n");
for(i=0;i< n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
/* Reversing */
for(i=0;i< n/2;i++)
{
temp = a[i];
a[i] = a[n-1-i];
a[n-1-i] = temp;
}
/* Displaying result */
printf("Reversed array is:\n");
for(i=0;i< n;i++)
{
printf("%d\t", a[i]);
}
return(0);
}
Output
Enter n: 4↲ Enter numbers: a[0] = 21↲ a[1] = 11↲ a[2] = 13↲ a[3] = 12↲ Reversed array is: 12 13 11 21