Write a program which asks the user for four numbers and the

Write a program which asks the user for four numbers and then tells them one of the following:
· That there were more even numbers than odd numbers
· That there were more odd numbers than even numbers
· That there were the same number of even and odd numbers
绿野仙踪2 1年前 已收到1个回答 举报

newcard 幼苗

共回答了18个问题采纳率:100% 举报

#include
void main(void)
{
int n,number[4],even,odd;
even=odd=0;
printf("please input 4 interger numbers:n");
for(n=0;n

1年前 追问

1

绿野仙踪2 举报

能排下版吗? 谢谢

举报 newcard

VC6标准代码模式,你可以自行调整,如需要解释我等下重新加上注释

绿野仙踪2 举报

我用的是python2.7. 直接复制好像不行。。

举报 newcard

那我再发一下代码,增加了注释,自己看看能否用。可能平台不同,程序有所不同,具体python2.7怎么用我也不清楚。你这个发到C/C++了。 #include void main(void)//vc6平台通过,其他平台可能为main()即可 { int n,number[4],even,odd; even=odd=0; printf("please input 4 interger numbers:n");//提示请输入4个整数 for(n=0;n<=3;n++) { scanf("%d",&number[n]);//请输入4个整数 } for(n=0;n<=3;n++) { if(number[n]%2==0) even++;//统计偶数数目 else if(number[n]%2==1) odd++;//统计奇数数目 } if(even==odd)//如果奇数偶数相等 printf("That there were the same number of even and odd numbersn"); else if (even>odd)//如果偶数多于奇数 printf("That there were more even numbers than odd numbersn"); else if(even

绿野仙踪2 举报

for(n=0;n<=3;n++) { scanf("%d",&number[n]);//请输入4个整数 } for(n=0;n<=3;n++) { if(number[n]%2==0) even++;//统计偶数数目 else if(number[n]%2==1) odd++;//统计奇数数目 } [我就是这里看不明白 其他的都挺清楚, 你能解释一下吗?]

举报 newcard

第一段:因为需要输入四个数字,就设立了一个数组,通过for循环实现每个数组元素的赋值; 第二段:for循环实现每个数组元素进行判断奇偶数,并累计 附加:可通过改变改变数组大小的方法实现更多的数字判断,因为你的描述是清晰的,故简化了程序
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.011 s. - webmaster@yulucn.com