C Program to Reversing an Array Elements in C Programming
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include<stdio.h> int main() { int arr[30], i, j, num, temp; printf("\nEnter no of elements : "); scanf("%d", &num); //Read elements in an array for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } j = i - 1; // j will Point to last Element i = 0; // i will be pointing to first element while (i < j) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; // increment i j--; // decrement j } //Print out the Result of Insertion printf("\nResult after reversal : "); for (i = 0; i < num; i++) { printf("%d \t", arr[i]); } return (0); } |
Output :
1 2 3 |
Enter no of elements : 5 11 22 33 44 55 Result after reversal : 55 44 33 22 11 |