C++程序循环语句

it2023-11-26  61

c++程序循环语句 让用户输入一个自然数x,计算并输出区间[1,x]之间的偶数和.,奇数和。

#include using namespace std; int main() { int x = 1,a=1; int ou=0, ji=0; cin >> x; while (a <= x) { if (a % 2 == 0) ou += a; else if (a % 2 == 1) ji += a; ++a; } cout << ou << ’ ’ << ji; }

最新回复(0)