How to print the elements of an array in reverse order in C?

PHPz
Release: 2023-08-26 17:45:19
forward
834 people have browsed it

How to print the elements of an array in reverse order in C?

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]); }

Copy after login

Example

The following is the C program to reverse the elements −

Online Demonstration

#include 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]); } }

Copy after login

Output

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
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn