Using a for statement to sum a sequence of integers. - C++ Statement

C++ examples for Statement:for

Description

Using a for statement to sum a sequence of integers.

Demo Code

#include <iostream>

int main(int argc, const char *argv[]) {
    int limit, num, total = 0;

    std::cout << "Enter number of values to be summed followed by values: ";
    std::cin >> limit;/*from   w  w w.j av  a  2 s .c om*/

    for (int i = 1; i <= limit; i++) {
        std::cin >> num;
        total += num;
    }

    std::cout << "Total = " << total << std::endl;
    return 0;
}

Result


Related Tutorials