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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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