
尝试按照下面给出的算法以相反的顺序打印元素:
步骤1 - 声明一个大小为5的数组
步骤2 - 使用for循环将5个元素输入到内存中
步骤3 - 以相反的顺序显示元素
立即学习“C语言免费学习笔记(深入)”;
通过递减for循环
唯一的逻辑是通过for循环反转元素:
for(i=4;i>=0;i--){
//Displaying O/p//
printf("array[%d] :",i);
printf("%d</p><p>",array[i]);
}以下是反转元素的C程序−
在线演示
#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: </p><p>");
//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 :</p><p>");
for(i=4;i>=0;i--){
//Displaying O/p//
printf("array[%d] :",i);
printf("%d</p><p>",array[i]);
}
}当上述程序被执行时,它产生以下结果 −
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
以上就是如何在C语言中将数组的元素以相反的顺序打印出来?的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号