C++ 实例 – 实现两个数相加
最后更新于:2022-03-27 02:33:40
C++ 实例 – 实现两个数相加
使用 C++ 获取用户的输入两个数字,并将两个数字相加,然后输出到屏幕:
实例
#include <iostream>
using namespace std; int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
cin >> firstNumber >> secondNumber;
sumOfTwoNumbers = firstNumber + secondNumber;
cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers;
}
using namespace std; int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
cout << "输入两个整数: ";
cin >> firstNumber >> secondNumber;
// 相加
sumOfTwoNumbers = firstNumber + secondNumber;
// 输出
cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers;
return 0;
}
以上程序执行输出结果为:
输入两个整数: 4 5 4 + 5 = 9