for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jvzj017
    New Member
    • Oct 2006
    • 10

    for loop

    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 .
  • dynamicleo
    New Member
    • Oct 2006
    • 14

    #2
    Originally posted by Jvzj017
    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 .
    int i, result = 0, lo = 10, hi = 20;
    for (i=lo; i<=hi; i++){
    result += i;
    }

    Comment

    • D_C
      Contributor
      • Jun 2006
      • 293

      #3
      It's too bad you have to use a for loop :).
      Code:
      result = (hi*hi + hi - lo*lo - lo)/2;

      Comment

      Working...