- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to print the elements in a reverse order from an array in C?
Try to print the elements in reverse order by following an algorithm given below −
Step1 − Declare an array of size 5
Step 2 − Enter the 5 elements in memory using for loop
Step 3 − Display elements in reverse order
By decrementing for loop
The only logic is to reverse the elements is For loop −
for(i=4;i>=0;i--){ //Displaying O/p// printf("array[%d] :",i); printf("%d
",array[i]); }
Example
Following is the C program to reverse the elements −
#include<stdio.h> void main(){ //Declaring the array - run time// int array[5],i; //Reading elements into the array// printf("Enter elements into the array:
"); //For loop// for(i=0;i<5;i++){ //Reading User I/p// printf("array[%d] :",i); scanf("%d",&array[i]); } //Displaying reverse order of elements in the array// printf("The elements from the array displayed in the reverse order are :
"); for(i=4;i>=0;i--){ //Displaying O/p// printf("array[%d] :",i); printf("%d
",array[i]); } }
Output
When the above program is executed, it produces the following result −
Enter elements into the array: array[0] :23 array[1] :13 array[2] :56 array[3] :78 array[4] :34 The elements from the array displayed in the reverse order are: array[4] :34 array[3] :78 array[2] :56 array[1] :13 array[0] :23
Advertisements