82.7k views
4 votes
Assume the int variables i,lo, hi, and result have been declared and that lo and hi have been initialized. Assume further that result has been initialized to the value 0. Write a for loop that adds the integers from lo up through hi (inclusive), and stores the result in result. Your code should not change the values of lo and hi. Also, do not declare any additional variables -- use only i,lo, hi, and result.

User Macloving
by
6.3k points

1 Answer

1 vote

Answer:

#include <iostream>

using namespace std;

int main() {

int i,lo=12,hi=45,result=0;//all the variables declared..

for(i=lo;i<=hi;i++)//for loop.

{

result+=i;//adding integers to sum..

}

cout<<result<<endl;//printing the result.

return 0;

}

Output:-

969

Step-by-step explanation:

The above-written program is in C++.The variables i,lo,hi,result are declared first only lo,hi,result are initialized.Then a for loop is used to iterate over the values from lo to hi for that i used and the value of every integer is added to the result.

User Mgokhanbakal
by
6.2k points