C++ 实例 - 实现两个数相加
使用 C++ 获取用户的输入两个数字,并将两个数字相加,然后输出到屏幕:
实例#include <iostream>
using namespace std;
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
cout << "输入两个整数: ";
cin >> firstNumber >> secondNumber;
// 相加
sumOfTwoNumbers = firstNumber + secondNumber;
// 输出
cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers;
return 0;
}
#include <iostream>
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