201k views
4 votes
c++ Write a for loop that computes the following sum: 5+10+15+20+...+485+490+495+500. The sum should be placed in a variable sum that has already been declared and initialized to 0. In addition, there is another variable, num that has also been declared. You must not use any other variables.

1 Answer

6 votes

Answer:

#include <iostream>

#include <cstdlib>

using namespace std;

int main(){

int sum=0, num=5; //variables declaration and inicialization

while (sum<500){ //conditioning to do the sum up to 500

sum=sum+num; //actually sum process

cout << "The value is: "<<sum; //show the result in screen

};

return 0;

}

User Deeenes
by
6.0k points