Answers for "how to add numbers in for loop c++"

C++
0

how to add numbers in for loop c++

#include <iostream>

int main() {
  int n = 0;
  int sum = 0;
  std::cout << "Please enter and integer: ";
  std::cin >> n;

  //this will ensure that the integer is posetive.
  while (n < 0 ) {
    std::cout <<"Please enter positive integer only.\n";
    std::cout << "Please enter and integer: ";
    std::cin >> n;
  }

  for (int i = 0; i < n ; i++) {
    std::cout << i << "\n";
    sum +=i
  }
  std::cout << "Sum of odd numbers is " << sum <<".\n";

}
Posted by: Guest on February-16-2021

Browse Popular Code Answers by Language