C Program to Reverse an Array

How to write a C Program to Reverse an Array using While Loop, For loop, Functions, and Recursion with example?

C Program to Reverse an Array using While Loop

This program allows the user to enter the size and the elements. Next, this C program to reverse array will reverse the given elements using the While Loop.

#include<stdio.h>
 
int main() 
{
   int a[100], i, j, Size, Temp;
 
   printf("\nPlease Enter the size : ");
   scanf("%d",&Size);
 
   //Inserting elements in to it
   for (i = 0; i < Size; i++) 
   {
      scanf("%d", &a[i]);
   }
 
   j = i - 1;   // Assigning j to Last element
   i = 0;       // Assigning i to first element
 
   while (i < j) 
   {
      Temp = a[i];
      a[i] = a[j];
      a[j] = Temp;
      i++;             
      j--;         
   }
 
   printf("\nResult is: ");
   for (i = 0; i < Size; i++) 
   {
      printf("%d \t", a[i]);
   }
 
   return 0;
}

Using a while loop output.

C Program to Reverse an Array Items using While Loop

The following statements in this reverse array in the c program ask the user to enter the size and assign the user entered a value to the Size variable.

   printf("\nPlease Enter the size : ");
   scanf("%d",&Size);

The below For loop in the reverse program will help to iterate each cell present in a[5] array. For loop iteration will start at 0, and the condition inside the for loops (i < Size) will ensure the compiler does not exceed the array limit.

Scanf statement inside the C programming for loop will store the user entered values in every individual array element, such as a[0], a[1], a[2], a[3], and a[4]

   for (i = 0; i < Size; i++) 
   {
      scanf("%d", &a[i]);
   }

Next, The Condition inside the While loop will ensure that the i value is less than j. Inside the while loop, we performed the Swapping with the help of the third variable.

If you want to perform the swapping without any temporary variables, then please refer to the Swapping without using the Temp Variable article.

Last, We used the for loop to print the reversed array to the console output.

   for (i = 0; i < Size; i++) 
   {
      printf("%d \t", a[i]);
   }

C Program to Reverse an Array using For Loop

This program allows the user to enter the size and the elements. Then, this program will reverse the given array of elements using For Loop.

#include<stdio.h>
 
int main() 
{
   int a[100], b[100], i, j, Size;
 
   printf("\nPlease Enter the size of an array: ");
   scanf("%d",&Size);
 
   //Inserting elements into the array
   for (i = 0; i < Size; i++) 
   {
      scanf("%d", &a[i]);
   }
 
   for(i = Size-1, j = 0; i >= 0; i--, j++) 
   {
      b[j] = a[i];     
   }
 
   printf("\nResult of an Reverse array is: ");
   for (i = 0; i < Size; i++) 
   {
      printf("%d \t", b[i]);
   }
 
   return 0;
}

ANALYSIS: In this C example program, we just replaced the while loop of the reverse array with the For Loop. Apart from that, everything is the same.

C Program to Reverse an Array using For Loop

C Program to Reverse an Array using Functions

This program allows the user to enter the size and the elements. Next, this program will reverse the given array of elements using Functions.

#include<stdio.h>
void ArrayReverese(int a[], int Start, int End);
void printArray(int a[], int Size);

int main() 
{
   int a[100], i, Size;
 
   printf("\nPlease Enter the size : ");
   scanf("%d",&Size);
   
   //Inserting elements into the Declared one
   for (i = 0; i < Size; i++) 
   {
      scanf("%d", &a[i]);
   }

    ArrayReverese(a, 0, Size-1);
    printf("\nResult is: \n");
    printArray(a, Size); //Printing  
    return 0;
}

/* Function to Reverse the Given */
void ArrayReverese(int a[], int Start, int End)
{
   int Temp;
   while (Start < End) 
   {
      Temp = a[Start];
      a[Start] = a[End];
      a[End] = Temp;
      Start++;             
      End--;         
   }  
}     

/* Function to print the Output */
void printArray(int a[], int Size)
{
  int i;
  for (i = 0; i < Size; i++) 
   {
      printf("%d ", a[i]);
   }
  printf("\n");
}
C Program to Reverse an Array using Functions

When the compiler reaches the ArrayReverse (a, 0, Size – 1) line in the main() program, the compiler will immediately jump to the following function and execute the code inside that function. Here, it reverses the array.

void ArrayReverese(int a[], int Start, int End)

Next, we also declared one more function to print the Array. When the compiler reaches to printArray (a, Size) line in the main() program, the compiler will immediately jump to the following function and execute the code inside that function.

void printArray(int a[], int Size)

We already explained the Code or logic in the above example.

C Program to Reverse an Array using Recursion

This program allows you to enter the size and the elements and reverse the given array using Recursion.

#include<stdio.h>

void ArrayReverese(int a[], int Start, int End);
void printArray(int a[], int Size);

int main() 
{
   int a[100], i, Size;
   printf("\nPlease Enter the size : ");
   scanf("%d",&Size);
   //Inserting elements
   for (i = 0; i < Size; i++) 
   {
      scanf("%d", &a[i]);
   }
    ArrayReverese(a, 0, Size-1); 
    printf("\nResult is: \n");
    printArray(a, Size); //Printing A
    return 0;
}

/* Function to Reverse */
void ArrayReverese(int a[], int Start, int End)
{
   int Temp;
   if (Start < End)
   {
      Temp = a[Start];
      a[Start] = a[End];
      a[End] = Temp;
      ArrayReverese(a, Start + 1, End - 1);        
   }  
}     

/* Function to print the Output */
void printArray(int a[], int Size)
{
  int i;
  for (i = 0; i < Size; i++) 
   {
      printf("%d ", a[i]);
   }
  printf("\n");
}
C Program to Reverse an Array using Recursion

In this c program to reverse array items, When the compiler reaches the ArrayReverse (a, 0, Size – 1) line in the main() program, the compiler will immediately jump to the following function and execute the code inside that function. Here, it is reversing it.

void ArrayReverese(int a[], int Start, int End)

Within the above function, the If statement will check whether the Start position is less than the End position. If the condition is TRUE, then only statements inside this If block will execute.

Inside the If statement, we wrote the following statement to call the function Recursively with the updated value. If you miss this statement in this program, then after completing the first line, it will terminate.

ArrayReverese(a, Start + 1, End - 1);

Next, we also declared one more function to print the items. When the compiler reaches to printArray (a, Size) line in the main() program, the compiler jumps immediately to the following function and executes the code inside that function.

void printArray(int a[], int Size)

We already explained the Code or logic for reversing an array in the above example.

Comments are closed.