C program to Reverse an Array
Consider the following I/O samples to reverse an array.
SAMPLE INPUT: a = {10,20,50,60,70}
SAMPLE OUTPUT: a={70,60,50,20,10}
The algorithm used here is :
- Input the number of elements
- Input the array elements
- Traverse the array ---> for(i=n-1;i>=0;i--)
- Print the reversed array.
C PROGRAM:
#include<stdio.h>
int main()
{
int arr[100];
int i, n;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("The Reversed Array is:\n");
for(i=n-1;i>=0;i--) // traversing the array
printf("%d\t", arr[i] );
return (0);
}
OUTPUT:
The given source code in C program to Reverse an Array is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free.
If you have any feedback about this article and want to improve this, please comment in the comment section.
Comments
Post a Comment