我希望从这个程序给我一个+(a + 1)+(a + 2)... + b的答案

时间:2015-10-08 15:39:59

标签: c++ loops for-loop

  

我想从这个程序给我一个+(a + 1)+(a + 2)... + b的答案,但它   给我一个+ b!帮助!

#include <iostream>

using namespace std;

int main(){

    int a, b, c;

    cout << "Enter the value of the first number" << endl;
    cin >> a; cout << endl;
    cout << "Enter the value of the second number" << endl;
    cin >> b; cout << endl;

    for(a; c <= b; c++){

    c = c + 1;

    }

    cout << "The sum of the numbers between " << a << " and " << b << " is " << c << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您想要的代码在这里:

#include <iostream>

using namespace std;

int main(){

    int a, b, c=0;

    cout << "Enter the value of the first number" << endl;
    cin >> a; cout << endl;
    cout << "Enter the value of the second number" << endl;
    cin >> b; cout << endl;
    for(int i=a;i<=b;i++)  c+=i;//notice here !!!

    cout << "The sum of the numbers between " << a << " and " << b << " is " << c << endl;

    return 0;
}