180k views
5 votes
Use the following variables:i, lo, hi, and result. Assume that lo and hi each are associated with an int and that result refers to 0. 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.

User Toymakerii
by
4.4k points

1 Answer

2 votes

Answer:

public class num8 {

public static void main(String[] args) {

int lo =0;

int hi =10;

int result =0;

int i=0;

while(i>=lo && i<=hi){

result = result+i;

i++;

}

System.out.println(result);

}

}

Step-by-step explanation:

In the above code written in Java, the variables lo and hi have been declared and assigned the values of 0 and 10 respectively

The variable i is implemented as a counter for the control of the loop

The code finds the sum of numbers between lo (0) and hi (10) which evaluates to 55

In the while condition while(i>=lo && i<=hi) the counter i is incremented at every iteration by 1

User Nathaniel Jones
by
4.7k points