1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有整数的和为44。
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "请输入两个数字,系统将自动帮你计算这两个数字之间(包括这两个数字)所有整数的和:" << endl;
cout << "请输入第一个数字:";
cin >> a;
cout << "请输入第二个数字:";
cin >> b;
int c, d;//声明变量c,d,预设c比d大,方便计算
if (a > b) { c = a, d = b; }
if (a < b) { c = b, d = a; }
if (a == b) { cout << "您输入的两个数字一样大,那么和为:" << a + b << endl; system("pause"); return 0; }
int total = 0;//total为总和
for (c = c; c > d; c--)
{
total = total + c;//total等于之前和与c-1之后和相加
}
total = total + d;
cout << "按照你输入的数字计算,何为:" << total << endl;
system("pause");
return 0;