- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
C Program to Reverse an Array
In this tutorial, you will learn and get code for reversing an array in C. Reversing an array means
- The first element becomes the last, and the last element becomes the first.
- Similarly, the second element is transformed into the second last, and the second last element is transformed into the second.
- and so on.
For example, if the given array is:
2 6 4 8
then the inverse will be
8 4 6 2
Print the reverse of an array in C
Before we change the order of the array, let's make a program that takes 10 array elements and prints them in the reverse order.
To print the array elements in reverse order, start their indexing from last to first. That is, if the user has provided 10 array elements, then the array present at index number 9 will be printed first and the element present at index number 0 will be printed last. In this way, the array in reverse order gets printed as shown in the program given below:
#include<stdio.h> #include<conio.h> int main() { int arr[10], i; printf("Enter any 10 elements: "); for(i=0; i<10; i++) { scanf("%d", &arr[i]); } printf("\nThe array elements in reverse order:\n"); for(i=9; i>=0; i--) { if(i==0) printf("%d", arr[i]); else printf("%d, ", arr[i]); } getch(); return 0; }
The program given above was written in the Code::Blocks IDE; therefore, after a successful build and run, here is its sample run:
Supply any 10 array elements and press the ENTER key to see the given 10 array elements in reverse order, as shown in the second snapshot of the sample run:
C Reverse an Array
Now let's modify the above program in a way that it will receive the size of an array, say 5. It will then prompt you to enter 5 array elements. After receiving the array elements, this program will reverse the array and then print the array on the output. In the previous program, we have printed the array in reverse order without actually reversing it.
#include<stdio.h> #include<conio.h> int main() { int arr[50], size, i, j, temp; printf("Enter array size: "); scanf("%d",&size); printf("Enter %d array elements: ", size); for(i=0; i<size; i++) scanf("%d",&arr[i]); j=i-1; // now j will point to the last element i=0; // and i will be point to the first element while(i<j) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; i++; j--; } printf("\nReverse of the Array is:\n"); for(i=0; i<size; i++) printf("%d ",arr[i]); getch(); return 0; }
The program was written under the Code::Blocks IDE, and here is the first snapshot of the sample run:
Supply the size of the array, say 5, and then enter any 5 array elements, pressing the ENTER key to reverse the array. And print its reverse as shown in the second snapshot of the sample run:
Program Explained
- Set the size of the receive array to 5, for example.
- Then receive 5 array elements.
- Using a while loop, as the size of the array here is 5, the element present at last, that is, the element present at the 4th index, will be initialized to the 0th index, and the element present at the 0th index will be initialized to the 4th index, and then the 3rd will be initialized to the 1st, and the 1st will be initialized to the 3rd, and so on.
- In this way, we can reverse the array as shown in the above program.
- Finally, after reversing the array, print it.
C Print the Original and Reverse Arrays
Let's create one more program that will receive the size and elements of an array, and will print the array elements in original and reverse order:
#include<stdio.h> #include<conio.h> int main() { int arr[100], i, limit; printf("How many elements you want to store inside the array: "); scanf("%d", &limit); printf("Enter any %d elements: ", limit); for(i=0; i<limit; i++) { scanf("%d", &arr[i]); } printf("\nThe array elements are:\n"); for(i=0; i<limit; i++) { if(i==(limit-1)) printf("%d", arr[i]); else printf("%d, ", arr[i]); } printf("\nNow the array elements in reverse order:\n"); for(i=(limit-1); i>=0; i--) { if(i==0) printf("%d", arr[i]); else printf("%d, ", arr[i]); } getch(); return 0; }
Here is the sample run:
The same program in different languages
« Previous Program Next Program »