C Program to reverse array

Simple C program to reverse array
To reverse array, we swap elements, first element with last element, second element with second last element and so on. You can also reverse array using recursion.
 
#include<stdio.h>
int main()
{
 int n;
 printf("Enter ize of array: ");
 scanf("%d",&n);
 int arr[n],i=0;
 for(i=0;i<n;i++)
 {
  printf("Enter %d element: ",i+1);
  scanf("%d",&arr[i]);
 }
 printf("\nArray is : \n");
 for(i=0;i<n;i++)
 {
  printf("%d ",arr[i]);
 }
 for(i=0;i<n/2;i++)
 {
  int temp=arr[i];
  arr[i]=arr[n-1-i];
  arr[n-1-i]=temp;
 }
 printf("\nReverse array is : \n");
 for(i=0;i<n;i++)
 {
  printf("%d ",arr[i]);
 }
 return 0;
}
 
Another C Program to reverse array
 
#include<stdio.h>
int main()
{
 int n;
 printf("Enter ize of array: ");
 scanf("%d",&n);
 
 int arr[n],i=0,j=n-1;
 for(i=0;i<n;i++)
 {
  printf("Enter %d element: ",i+1);
  scanf("%d",&arr[i]);
 }
 printf("\nArray is : \n");
 for(i=0;i<n;i++)
 {
  printf("%d ",arr[i]);
 }
 for(i=0,j=n-1;i<j;i++,j--)
 {
  int temp=arr[i];
  arr[i]=arr[j];
  arr[j]=temp;
 }
 printf("\nReverse array is : \n");
 for(i=0;i<n;i++)
 {
  printf("%d ",arr[i]);
 }
 return 0;
}
 
C program to reverse array using recursion
 
#include<stdio.h>
void ReverseArray(int arr[],int n)
{
 if(n<0)
 return ;
 printf("%d ",arr[n]);
 ReverseArray(arr,n-1);
}
int main()
{
 int arr[7]={12,7,32,17,90,76,4};
 printf("Reverse array is : \n");
 ReverseArray(arr,6);
 return 0;
}




Popular posts from this blog