Home / Programs / C Program to Reversing an Array Elements in C Programming
Programming Example

C Program to Reversing an Array Elements in C Programming

👁 1,315 Views
💻 Practical Program
📘 Step by Step Learning
C Program to Reversing Array Elements in C Programming

Program Code

/* 
C Program to Reversing an Array Elements in C Programming
Author: Atnyla Developer

*/


#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


Enter no of elements : 5
5
4
3
2
1

Result after reversal : 1       2       3       4       5  

Explanation

C Program to Reversing Array Elements in C Programming

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.