5.3 Introduction to Loops: The Do While Loop a posttest loop
40181
Given an Integer variable n that has been initialized to a positive value and, in addition, Integer variables k and total that have already been declared, use a While loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total.
k = 0
total = 0
Do While (k <= n)
total = (total + (k*k*k))
k += 1
Loop
total = 0
Do While (k <= n)
total = (total + (k*k*k))
k += 1
Loop
40178
Given an Integer variable k that has already been declared, use a pretest Do While loop to print a single line consisting of 88 asterisks. Use no variables other than k.
Do While k < 88
Console.Write("*")
k+= 1
Loop
Console.Write("*")
k+= 1
Loop
40179
Given an Integer variable n that has already been declared and initialized to a positive value, use a pretest Do While loop to print a single line consisting of n asterisks. Use no variables other than n.
Do While (n > 0)
Console.Write("*")
n -= 1
Loop
Console.Write("*")
n -= 1
Loop
40387
Write a statement that increments the value of the int variable total by the value of the int variable amount. That is, add the value of amount to total and assign the result to total.
total += amount
40180
Given Integer variables k and total that have already been declared, use a pretest Do While loop to compute the sum of the squares of the first 50 whole numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
k = 1
total = 0
Do While (k <= 50)
total = (total + (k*k))
k += 1
Loop
total = 0
Do While (k <= 50)
total = (total + (k*k))
k += 1
Loop
40379
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a while loop to compute the sum of the cubes of the first n counting numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total. Do NOT modify n.
k = 0
total = 0
Do While (k <= n)
total = (total + (k*k*k))
k += 1
Loop
total = 0
Do While (k <= n)
total = (total + (k*k*k))
k += 1
Loop
40378
Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
k = 1
total = 0
Do While (k <= 50)
total = (total + (k*k))
k += 1
Loop
total = 0
Do While (k <= 50)
total = (total + (k*k))
k += 1
Loop
5.4 The Do Until and For...Next Loops
40182
Given an Integer variable k that has already been declared, use a posttest Do While loop to print a single line consisting of 53 asterisks. Use no variables other than k.
Do
Console.Write("*")
k+= 1
Loop While k < 53
Console.Write("*")
k+= 1
Loop While k < 53
40183
Given an int variable n that has already been declared and initialized to a positive value, use a posttest Do While loop to print a single line consisting of n asterisks. Use no variables other than n.
Do
Console.Write("*")
n -= 1
Loop While n > 0
Console.Write("*")
n -= 1
Loop While n > 0
40184
Given int variables k and total that have already been declared, use a posttest Do While loop to compute the sum of the squares of the first 50 whole numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
k = 1
total = 0
Do While (k <= 50)
total = (total + (k*k))
k += 1
Loop
total = 0
Do While (k <= 50)
total = (total + (k*k))
k += 1
Loop
40174
Given an int variable k that has already been declared, use a for loop to print a single line consisting of 97 asterisks. Use no variables other than k.
For k = 1 To 97
Console.Write("*")
Next
Console.Write("*")
Next
40288
Write a for loop that computes the following sum: 5+10+15+20+...+485+490+495+500. The sum should be placed in a variable sum that has already been declared and initialized to 0. In addition, there is another variable , num that has also been declared. You must not use any other variables .
sum = 0
For num = 5 to 500 step 5
sum += num
Next
For num = 5 to 500 step 5
sum += num
Next
40185
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total.
total = 0
For k = 0 to n
total += k*k*k
Next
For k = 0 to n
total += k*k*k
Next
40385
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.
result = 0
For i = lo To hi
result += i
Next
For i = lo To hi
result += i
Next
40374
Given int variables k and total that have already been declared, use a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
total = 0
For k = 0 To 50
total = total + k*k
Next
For k = 0 To 50
total = total + k*k
Next
40375
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.
total = 0
For k = 1 to n
total += k*k*k
Next
For k = 1 to n
total += k*k*k
Next
40502
Assume the integer variables counter, low, high, and result have been declared and that low and high have been initialized.
Write a For loop that adds the integers between low and high (inclusive), and stores the result in result.
Your code should not change the values of low and high. Also, do not declare any additional variables -- use only counter, low, high, and result.
result = 0
For counter = low to high
result = result + counter
Next
For counter = low to high
result = result + counter
Next
40176
Given int variables k and total that have already been declared, use a for loop to compute the sum of the squares of the first 50 whole numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
total = 0
For k = 0 to 50
total = total + k*k
Next
For k = 0 to 50
total = total + k*k
Next
40177
Given an Integer variable n that has been initialized to a positive value and, in addition, Integer variables k and total that have already been declared, use a For loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total.
total = 0
For k = 1 to n
total += k*k*k
Next
For k = 1 to n
total += k*k*k
Next
40187
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 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.
For i = lo To hi
result += i
Next
result += i
Next
40223
Give that two Integer variables , total and amount have been declared, write a loop that reads non-negative values into amount and adds them into total. The loop terminates when a value less than 0 is read into amount. Don't forget to initialize total to 0.
To read a value into amount use a method, getNum() that we provide for you a class named TC:
amount = TC.getNum();
amount = TC.getNum();
total = 0
amount = 0
Do
total += amount
amount = TC.getNum()
Loop Until (amount < 0)
amount = 0
Do
total += amount
amount = TC.getNum()
Loop Until (amount < 0)