C Online Compiler
Example: Array Reversal using For Loop Function in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Array Reversal using For Loop Function #include <stdio.h> // Function to print the array void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } // Function to reverse the array using a for loop void reverseArrayForLoop(int arr[], int size) { int start = 0; int end = size - 1; while (start < end) { // Swap elements at start and end int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; // Move pointers towards the center start++; end--; } } int main() { // Step 1: Initialize an array int arr[] = {10, 20, 30, 40, 50}; int n = sizeof(arr) / sizeof(arr[0]); // Step 2: Print original array printf("Original array: "); printArray(arr, n); // Step 3: Reverse the array using the function reverseArrayForLoop(arr, n); // Step 4: Print reversed array printf("Reversed array (for loop): "); printArray(arr, n); return 0; }
Output
Clear
ADVERTISEMENTS