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 reverse array program allows the user to enter the size and the elements. Next, this C program to reverse array will reverse the given elements using 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;
}

C array reverse using a while loop output


Please Enter the size : 6
10 20 30 40 50 60

Result is: 60 	50 	40 	30 	20 	10 	

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

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

The below For loop in reverse array 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, not to 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 make sure 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 Swapping without using 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 of the reverse array allows the user to enter the size and the elements. Then, this program will reverse the given array 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 example, we just replaced the while loop of c reverse array with 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 reverse array program allows the user to enter the size and the elements. Next, this program will reverse the given array 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 array reverse using Functions output


Please Enter the size : 5
15 25 35 45 55

Result is: 
55 45 35 25 15 

When the compiler reaches to ArrayReverse (a, 0, Size – 1) line in the main() program, the compiler will immediately jump to the following function and executed the code inside that function. Here, it is reversing 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 executed 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. Next, this program will reverse the given array elements 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");
}

Please Enter the size : 10
5 10 15 20 25 30 35 40 45 50

Result is: 
50 45 40 35 30 25 20 15 10 5 

In this reverse array in c program, When the compiler reaches to ArrayReverse (a, 0, Size – 1) line in the main() program, the compiler will immediately jump to the following function and executed 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 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 reverse an array in c in the above example.

Comments are closed.