9.6k views
0 votes
WHILE Loops > while loops > 51187 Deadline: 03/06/23 10:45am ES Instructions In this exercise, use the following variables: i,10,hi, and result. Assume that lo and hi each are associated with in int and that result refers to θ. Write a while loop that adds the integers from lo up through hi (inclusive), and associates the sum with result. Your code should not change the values associated with lo and hi. Also, just use these variables: i,lo, hi, and result. Additional Notes: hi and lo should not be modified

1 Answer

3 votes

Final answer:

To calculate the sum of integers from lo to hi using a while loop, initiate result to 0, set i to lo, and loop until i exceeds hi, incrementing both result and i in each iteration.

Step-by-step explanation:

To write a while loop that adds the integers from lo up through hi (inclusive), and associates the sum with result, without changing the values associated with lo and hi, your code in a programming language like Python could look something like this:

result = 0
i = lo
while i <= hi:
result += i
i += 1

This loop will continue to execute as long as i is less than or equal to hi, adding the value of i to result during each iteration. After adding, it increments i by 1. When i exceeds hi, the loop will terminate, and result will contain the sum of all integers between lo and hi inclusive.

User Hasib Samad
by
7.1k points