C Program to Reverse an Array

C Program to Reverse an Array

C program to reverse an array; Through this tutorial, we will learn how to reverse an array using for loop, while loop and pointer in c programs.

Programs to Reverse an Array in C

  • 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 output 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 output 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 output 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

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *