negia
1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,t;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(int i=0;i<n;i++)
{
t=arr[i];
arr[i]=arr[n-1-i];
arr[n-1-i]=t;
}
for(int i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}
Run a loop till n/2 only (in which you are swapping the elements).
Running a loop till n, means you are reversing a reversed array.
for (int i = 0; i < n/2; i++) // first half elements will be swapped with rest half
{
t = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = t;
}
1 Like
YOU CAN USE while loop with condition start<end and swap the elements,it is best way for reversal