Try to print the elements in reverse order as per the algorithm given below:
Step 1- Declare an array of size 5
Step 2- Use a for loop to input the 5 elements into memory
Step 3- Display the elements in reverse order
For loop by decrement
The only logic is to reverse the elements through the for loop:
for(i=4;i>=0;i--){ //Displaying O/p// printf("array[%d] :",i); printf("%d",array[i]); }
The following is the C program to reverse the elements −
Online Demonstration
#includevoid 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]); } }
When the above program is executed, it produces the following results−
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
The above is the detailed content of How to print the elements of an array in reverse order in C?. For more information, please follow other related articles on the PHP Chinese website!