# Chapter 5

## Unlock all answers in this set

question
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.
total=0; k=50; while (k>0) { total+=k*k; k--; }
Explanation: The given code will compute the sum of the squares of the first 50 counting numbers and store the value in the total variable. The code will loop through each number from 1 to 50, compute the square of the number, and add it to the total. The final value in the total variable will be the sum of the squares of the first 50 counting numbers.
question
Given an int variable k that has already been declared, use a while loop to print a single line consisting of 88 asterisks. Use no variables other than k.
k = 1; while (k <= 88) { System.out.print('*'); k++; } System.out.println();
question
Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
n = -1; while (n < 1 || n > 10) n = stdin.nextInt();
Explanation: Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.The code would look something like this:while (true) { System.out.print(Enter a number between 1 and 10: ")
question
Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines, write the code necessary to read in every line and print them all out on a single line, separated by a space. Hint: Use input.nextLine() to read a line and use input.hasNextLine() to test if the input has a next line available to read.
while (input.hasNextLine()) { System.out.print(input.nextLine()); if (input.hasNextLine()) System.out.print(" "); } System.out.println(); Correct
question
Given an int variable n that has already been declared and initialized to a positive value, use a while loop to print a single line consisting of n asterisks. Use no variables other than n.
while (n > 0) { System.out.print('*'); n--; } System.out.println();
Explanation: The code to print a single line consisting of n asterisks is as follows:while (n > 0) { System.out.print(*")
question
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each followed by a space, and that terminates when it reads an integer that is not positive. Declare any variables that are needed. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
int x; x = stdin.nextInt(); while (x > 0) { if (x > 100) System.out.print(x + " "); x = stdin.nextInt(); }
question
Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is considered a consecutive duplicate. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7. Write some code that uses a loop to read such a sequence of non-negative integers , terminated by a negative number. When the code finishes executing, the number of consecutive duplicates encountered is printed. In this case, 3 would be printed. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
int n, prev, consecdups = 0; prev = stdin.nextInt(); n = stdin.nextInt(); while (n >= 0) { if (n == prev) consecdups++; prev = n; n = stdin.nextInt(); } System.out.println(consecdups);
question
Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of strings and two int variables, count and longest, write the code necessary to examine all the strings in the input source and determine how long the longest string (or strings are). That value should be assigned to longest. The number ﻿of strings that are of that length should be assigned to count. For a hint on this quiz, please check https://liveexample.pearsoncmg.com/supplement/REVELProjectHint.pdf.
count = 0; longest = 0; while (input.hasNext()) { String s = input.next(); if (s.length() > longest) { longest = s.length(); count = 1; } else if (s.length() == longest) count++; }
Explanation: The Scanner class has a method called nextLine() that returns the next complete line of input from the input source. The Scanner class also has a method called hasNextLine() that returns true if there is another line of input and false if there is not.We can use a while loop to iterate through all the strings in the input source. For each string, we can use the length() method to determine how long it is. If it is longer than the longest string we have seen so far, we can update the value of longest. We can also keep track of how many strings are the same length as the longest string using a variable called count.
question
Given an int variable k that has already been declared, use a do...while loop to print a single line consisting of 53 asterisks. Use no variables other than k.
k = 0; do { System.out.print('*'); k++; } while(k < 53); System.out.println();
Explanation: The code would look something like this:do { cout << *"
question
Given an int variable n that has already been declared and initialized to a positive value, use a do...while loop to print a single line consisting of n asterisks. Use no variables other than n.
do { System.out.print('*'); } while(--n > 0); System.out.println();
question
Given int variables k and total that have already been declared, use a do...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.
total = 0; k = 50; do { total += k*k; k--; } while (k > 0);
question
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; k = n; do { total += k * k * k; k--; } while(k > 0);
Explanation: The sum of the cubes of the first n whole numbers is given by the equation:sum = k(k+1)(k+2)where k is the number of terms in the sum. In this case, we want the sum of the first n cubes, so k = n. We can rewrite the equation as:sum = n(n+1)(n+2)We can compute the sum using a do...while loop as follows:int n = 1; // initialize n to 1int k = n; // initialize k to nint total = 0; // initialize total to 0do { total = total + k*(k+1)*(k+2); // compute sum of first n cubes k = k + 1; // increment k by 1}while (k <= n); // repeat while k is less than or equal to n
question
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 = 1; k <= 50; k++) total += k * k; Correct
Explanation: for (k = 1; k <= 50; k++) { total = total + k*k;}
question
Write a for loop that prints in ascending order all the positive multiples ﻿of 5 that are less than 175, separated by exactly one space.
for (int k = 5; k < 175; k += 5) System.out.print(k + " ");
Explanation: ;To write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, we can use the following code:for(int i=5
question
Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by exactly one space.
for (int k = 80; k >= 20; k -= 2) System.out.print(k + " ");
Explanation: ;The for loop should print the integers in descending order.for (int i = 80
question
Write a for loop that prints in ascending order all the positive integers less than 200 that are divisible by both 2 and 3, separated by exactly one space.