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 :

    1. Input the number of elements
    2. Input the array elements
    3. Traverse the array  ---> for(i=n-1;i>=0;i--)
    4. 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

Popular posts from this blog

INVERTED PYRAMID STAR PATTERN

C PROGRAM TO PRINT ALL LEAP YEARS IN A GIVEN RANGE

C PROGRAM TO PRINT THE REMAINING DAYS AND COMPLETED DAYS FROM A GIVEN DATE