数组是一组以单一名称存储的相关数据项。
例如 int Student[30]; //student是一个数组名,包含单个变量名的30个数据项集合
数组的操作
搜索 – 用于查找特定元素是否存在
排序 – 它有助于排列数组中的元素按升序或降序排列。
立即学习“C语言免费学习笔记(深入)”;
遍历 – 它按顺序处理数组中的每个元素。
插入 – 它有助于在数组中插入元素。
删除 – 它有助于删除数组中的元素。数组中的元素。
在数组中查找偶数的逻辑如下 –
for(i = 0; i在数组中查找奇数的逻辑如下 -
for(i = 0; i要显示偶数,请调用下面提到的显示函数 -
printf("no: of elements comes under even are = %d", Ecount);printf("The elements that are present in an even array is: ");void display(int a[], int size){ int i; for(i = 0; i
");}
登录后复制
要显示奇数,请按照以下方式调用显示函数 −
printf("no: of elements comes under odd are = %d", Ocount);printf("The elements that are present in an odd array is : ");void display(int a[], int size){ int i; for(i = 0; i
");}
登录后复制
程序
以下是使用 for 循环分隔数组中偶数和奇数的 C 程序 -
现场演示
#includevoid display(int a[], int size);int main(){ int size, i, a[10], even[20], odd[20]; int Ecount = 0, Ocount = 0; printf("enter size of array :"); scanf("%d", &size); printf("enter array elements:
"); for(i = 0; i
", Ecount); printf("The elements that are present in an even array is: "); display(even, Ecount); printf("no: of elements comes under odd are = %d
", Ocount); printf("The elements that are present in an odd array is : "); display(odd, Ocount); return 0;}void display(int a[], int size){ int i; for(i = 0; i
");}
登录后复制
输出
当执行上述程序时,会产生以下结果 -
enter size of array:5enter array elements:2345671234no: of elements comes under even are = 2The elements that are present in an even array is: 12 34no: of elements comes under odd are = 3The elements that are present in an odd array is : 23 45 67登录后复制
以上就是如何使用C语言中的for循环将数组中的偶数和奇数分开?的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2588085.html