In this article, we are going to learn how to reverse arrays element-wise in C language. We will take an array and then we will reverse its elements one by one and will display the fully reversed array.
1. C Program to reverse an array using another array
#include <stdio.h>
int main()
{
int arr[10], num ;
printf("Please enter the Size of array with Max size 10: ");
scanf("%d",&num);
printf("Please enter the elements in this Array: ");
for(int i=0; i < num; i++)
scanf("%d",&arr[i]);
int reverseArr[num];
int i = 0;
int j = num-1;
while(j>=0)
{
reverseArr[i] = arr[j];
i++;
j--;
}
printf("\nThe Reverse of array entered by you is: \n");
for (int i = 0; i < num; ++i)
{
printf("%d ", reverseArr[i]);
}
return 0;
}
Output
Please enter the Size of array with Max size 10: 5
Please enter the elements in this Array: 3 6 9 12 15\
The Reverse of array entered by you is:
15 12 9 6 3
2. C Program to reverse an array using another array while loop
#include <stdio.h>
int main()
{
int arr[10], num ;
printf("Please enter the Size of array with Max size 10: ");
scanf("%d",&num);
printf("Please enter the elements in this Array: ");
for(int i=0; i < num; i++)
scanf("%d",&arr[i]);
int reverseArr[num];
for (int i = 0, j=num-1; j>=0; i++, j--)
{
reverseArr[i] = arr[j];
}
printf("\nThe Reverse of array entered by you is: \n");
for (int i = 0; i < num; ++i)
{
printf("%d ", reverseArr[i]);
}
return 0;
}
Output
Please enter the Size of array with Max size 10: 5
Please enter the elements in this Array: 3 6 9 12 15
The Reverse of array entered by you is:
15 12 9 6 3