C Program to Reverse an Array
In this post, you will learn how to reverse an array using the C programming language. Such a type of question is generally asked in programming interviews. Here, we have mentioned different C program to find the reverse of an array.
C program to reverse an array using Recursion
A recursion function is a function that is called by itself. In the given C program, we call the recursion function to get the reverse of an array. A recursion function continues until some condition is met to prevent it. That's why we use the if statement to break the infinite recursion.
// Recursive C program to reverse an array
#include<stdio.h>
// Function to reverse an array
void reverseArray(int arr[], int start, int end)
{
int temp;
if (start >= end)
return;
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
reverseArray(arr, start+1, end-1);
}
// Function to print an array
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver function
int main()
{
int arr[] = {3, 5, 2, 9, 8, 4};
printf("Given array: \n");
printArray(arr, 6);
reverseArray(arr, 0, 5);
printf("Reversed array: \n");
printArray(arr, 6);
return 0;
}
Output of the above code:
Given array:
3 5 2 9 8 4
Reversed array:
4 8 9 2 5 3
C program to reverse an array using iteration
The given C program reverses the given array elements using a while loop.
// C program to reverse an array
// using iteration
#include<stdio.h>
// Function to reverse an array
void reverseArray(int arr[], int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
// Function to print an array
void printArray(int arr[], int size)
{
int x;
for (x=0; x < size; x++)
printf("%d ", arr[x]);
printf("\n");
}
// Driver function
int main()
{
int arr[] = {4, 2, 5, 6, 3, 8};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Given array: \n");
printArray(arr, n);
reverseArray(arr, 0, n-1);
printf("Reversed array: \n");
printArray(arr, n);
return 0;
}
Output of the above code:
Given array:
4 2 5 6 3 8
Reversed array:
8 3 6 5 2 4
C program to reverse an array using for loop
The given C program asked the user to enter the elements of an array and then used the for loop to reverse the given array elements.
#include<stdio.h>
int main()
{
int arr[5], x;
printf("Enter any 5 elements: ");
for(x=0; x<5; x++)
{
scanf("%d", &arr[x]);
}
printf("\nThe array elements in reverse order:\n");
for(x=4; x>=0; x--)
{
if(x==0)
printf("%d", arr[x]);
else
printf("%d, ", arr[x]);
}
return 0;
}
Output of the above code:
Enter any 5 elements: 5 3 7 2 1
The array elements in reverse order:
1, 2, 7, 3, 5
C program to reverse an array using pointer
Here, we have used a pointer to reverse an array.
#include<stdio.h>
#include<stdlib.h>
void reverse_array(int*, int);
int main()
{
int n, c, *pointer;
scanf("%d",&n);
pointer = (int*)malloc(sizeof(int)*n);
if( pointer == NULL )
exit(EXIT_FAILURE);
for ( c = 0 ; c < n ; c++ )
scanf("%d",(pointer+c));
reverse_array(pointer, n);
printf("Original array on reversal is\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n",*(pointer+c));
free(pointer);
return 0;
}
void reverse_array(int *pointer, int n)
{
int *s, c, d;
s = (int*)malloc(sizeof(int)*n);
if( s == NULL )
exit(EXIT_FAILURE);
for ( c = n - 1, d = 0 ; c >= 0 ; c--, d++ )
*(s+d) = *(pointer+c);
for ( c = 0 ; c < n ; c++ )
*(pointer+c) = *(s+c);
free(s);
}
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative