Department of Computer Science | Institute of Theoretical Computer Science | CADMO

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

// Program: sum_n.cpp // Compute the sum of the first n natural numbers. #include int main() { // input std::cout << "Compute the sum 1+...+n for n =? "; unsigned int n; std::cin >> n; // computation of sum_{i=1}^n i unsigned int s = 0; for (unsigned int i = 1; i <= n; ++i) s += i; // output std::cout << "1+...+" << n << " = " << s << ".\n"; return 0; }