how to add numbers in for loop c 2b 2b

Solutions on MaxInterview for how to add numbers in for loop c 2b 2b by the best coders in the world

showing results for - "how to add numbers in for loop c 2b 2b"
Justina
04 Oct 2016
1#include <iostream>
2
3int main() {
4  int n = 0;
5  int sum = 0;
6  std::cout << "Please enter and integer: ";
7  std::cin >> n;
8
9  //this will ensure that the integer is posetive.
10  while (n < 0 ) {
11    std::cout <<"Please enter positive integer only.\n";
12    std::cout << "Please enter and integer: ";
13    std::cin >> n;
14  }
15
16  for (int i = 0; i < n ; i++) {
17    std::cout << i << "\n";
18    sum +=i
19  }
20  std::cout << "Sum of odd numbers is " << sum <<".\n";
21
22}