C Program to Reverse an Array

In this tutorial, i am going to show you how to reverse an array with the help of for loop, while loop and pointer in c programs.

All C Programs to Reverse an Array

  • C Program to Reverse an Array using For Loop
  • C Program to Reverse an Array using While Loop
  • C Program to Reverse an Array using Pointer

C Program to Reverse an Array using For Loop

#include<stdio.h>
 
int main() 
{
   int a[100], b[100], i, j, Size;
 
   printf("\nPlease Enter the size of an array: ");
   scanf("%d",&Size);
 
   printf("\nPlease Enter array elements: ");
  
   //Inserting elements into the array
   for (i = 0; i < Size; i++) 
   {
      scanf("%d", &a[i]);
   }
 
   for(i = Size-1, j = 0; i >= 0; i--, j++) 
   {
      b[j] = a[i];     
   }
 
   printf("\nResult of an Reverse array is: ");
   for (i = 0; i < Size; i++) 
   {
      printf("%d \t", b[i]);
   }
 
   return 0;
}

The result of the above c program; as follows:

Please Enter the size of an array: 5
Please Enter array elements: 7 8 9 1 3
Result of an Reverse array is: 3 	1 	9 	8 	7 

C Program to Reverse an Array using While Loop

#include<stdio.h>
 
int main() 
{
   int a[100], i, j, Size, Temp;
 
   printf("\nPlease Enter the size of an array: ");
   scanf("%d",&Size);
   
   printf("\nPlease Enter array elements: ");
 
   //Inserting elements into the array
   for (i = 0; i < Size; i++) 
   {
      scanf("%d", &a[i]);
   }
 
   j = i - 1;   // Assigning j to Last array element
   i = 0;       // Assigning i to first array element
 
   while (i < j) 
   {
      Temp = a[i];
      a[i] = a[j];
      a[j] = Temp;
      i++;             
      j--;         
   }
 
   printf("\nResult of an Reverse array is: ");
   for (i = 0; i < Size; i++) 
   {
      printf("%d \t", a[i]);
   }
 
   return 0;
}

The result of the above c program; as follows:

Please Enter the size of an array: 5
Please Enter array elements: 1 2 3 4 5
Result of an Reverse array is: 5 	4 	3 	2 	1 	

C Program to Reverse an Array using Pointer

#include <stdio.h>
#include <stdlib.h>
int main()
{
	
	int *a,n,i,j,temp;
	printf("Enter size of array:");
	scanf("%d",&n);
	a=calloc(sizeof(int),n);
	printf("Enter %d Elements:",n);
	for(i=0;i<n;i++)
	{
		scanf("%d",a+i);
	}
	
		
	for(i=0,j=n-1;i<j;i++,j--)
	
	{
		temp=*(a+i);
		*(a+i)=*(a+j);
		*(a+j)=temp;
	}
	printf("After reversing the array:\n");
	for(i=0;i<n;i++)
	{
		printf("%d",*(a+i));
	}
	
	return 0;
}

The result of the above c program; as follows:

Enter size of array:5
Enter 5 Elements:1 2 3 4 5
After reversing the array:
54321

More C Programming Tutorials

Leave a Comment