Ask Question
7 May, 15:37

Assume the int variables i, lo, hi, and result have been declared and that lo and hi have been initialized. Write a for loop that adds the integers between lo and 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.

+5
Answers (1)
  1. 7 May, 18:50
    0
    result=0;

    for (i=lo; i<=hi; i++) {

    result + = i;

    }

    Explanation:

    The question says result was declared but not initialized, so using result without initializing it to 0 would throw an error as result would be undefined and you can't add a number to undefined

    Another way to do it to keep everything inside the loop would be

    for (i=lo; i<=hi; i++) {

    if (i==lo) result = 0;

    result + = i;

    }

    but this is impractical since it would add unnecesary operations in each cycle of the loop
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Assume the int variables i, lo, hi, and result have been declared and that lo and hi have been initialized. Write a for loop that adds the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers