编写一个程序,求出整型数组a中偶数元素个数,并将其结果打印出来。数组为:
int a[6] = (8, 7,12,9,16,18);
望给个采纳,谢谢!
1、效果如下
#include<iostream>
using namespace std;
int main() {
int count = 0;
int a[6] = { 8, 7, 12, 9, 16, 18 };
for (int i = 0; i < 6; i++)
{
if (a[i] % 2 == 0) {
count += 1;
printf("%d\n", a[i]);
}
}
printf("偶数元素个数:%d\n", count);
system("\npause");
return 0;
}
#include <stdio.h>
int main()
{
int a[6] = {8, 7, 12, 9, 16, 18};
int count = 0;
for (int i = 0; i < 6; i++)
{
if (a[i] %2== 0)
{
count++;
}
}
printf("偶数元素个数为:%d\n", count);
return 0;
}
int a[6] = {8, 7, 12, 9, 16, 18};
int index = 0;
int count = 0;
int sz = sizeof(a) / sizeof(a[0]);
while (true) {
if (a[index] % 2 == 0) {
count+=1;
}
if (index >= sz - 1) {
break;
}
index++;
}
printf("%d",count);
写得都不出错,我的也是一种写法
遍历循环,除以2来判断偶数,满意请采纳,
#include <stdio.h>
int main()
{
int a[6] = {8, 7, 12, 9, 16, 18};
int count = 0;
for (int i = 0; i < 6; i++)
{
if (a[i] %! (MISSING)== 0)
{
count++;
}
}
printf("The number of even elements in the array is %!d(MISSING)\n", count);
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!