C++ 如何添加两个数字


两个数字相加

了解如何在 C++ 中将两个数字相加:

示例

int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
亲自试一试 »

通过用户输入将两个数字相加

在此示例中,用户必须输入两个数字。然后我们通过计算(相加)两个数字来打印总和:

示例

int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
运行示例 »