Q-30: Write a C/C++ program to reverse a given array.
    

Using - C


#include<stdio.h> void displayArray(int array[], int size) { int i; for(i=0; i < size; i++) { printf("%d ",array[i]); } printf("\n"); } void main() { int size, temp, i; printf("Enter the size of array: "); scanf("%d", &size); int array[size]; printf("Enter %d Elements:\n"); for(i=0; i < size; i++) { scanf("%d", &array[i]); } printf("ORIGINAL ARRAY: "); displayArray(array,size); for(i=0; i < size/2; i++) { temp = array[i]; array[i] = array[size-1-i]; array[size-1-i] = temp; } printf("REVERSED ARRAY: "); displayArray(array,size); }

OUTPUT

Enter the size of array:  6
Enter 6 Elements:
56   23   54   87   22   11
ORIGINAL ARRAY   :  56  23  54  87  22  11
REVERSED ARRAY : 11  22  87  54  23  56

        

Using - C++


#include<iostream> using namespace std; void displayArray(int array[], int size) { for(int i=0; i < size; i++) { cout<< array[i]<< " "; } cout<< endl; } int main() { int size, temp, i; cout<< "Enter the size of array: "; cin>> size; int array[size]; cout<< "Enter "<< size <<" Elements:"<< endl; for(i=0; i < size; i++) { cin>> array[i]; } cout<< "ORIGINAL ARRAY: "; displayArray(array,size); for(i=0; i < size/2; i++) { temp = array[i]; array[i] = array[size-1-i]; array[size-1-i] = temp; } cout<< "REVERSED ARRAY: "; displayArray(array,size); return 0; }

OUTPUT

Enter the size of array:  7
Enter 7 Elements:
4   5   2   3   9   8   7
ORIGINAL  ARRAY  :  4   5   2   3   9   8   7
REVERSED ARRAY:  7   8   9   3   2   5   4