MyProgrammingLab 5.3, 5.4, 5.5, 5.6, 5.9

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

You need to write a loop that will repeat exactly 125 times. Which is the preferred loop construct to use?

for loop

You need to write a loop that will keep reading and adding integers to a sum, until the sum reaches or exceeds 21. The numbers are all less than 20 and the sum is initially 0. Which is the preferred loop construct to use?

do while loop

Write a loop that displays all possible combinations of two letters where the letters are 'a', or 'b', or 'c', or 'd', or 'e'. The combinations should be displayed in ascending alphabetical order: aa ab ac ad ae ba bb ... ee

for (char i='a'; i<='e'; i++)for (char j='a'; j<='e'; j++) {System.out.print(i);System.out.println(j);}

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 from lo up through 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; i<=hi; i++){ result += i; }

Write a for loop that prints the integers 50 through 1, separated by spaces. Use no variables other than count.

for (int count=50; count>0; count--){ System.out.print(count + " "); }

Write a for loop that prints the odd integers 11 through 121 inclusive, separated by spaces.

for (int i=11; i<=121; i++){ if ((i%2)!=0){ System.out.print(i + " "); } }

Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, separated by spaces.

for (int i=1; i<175; i++){ if ((i % 5) == 0){ System.out.print(i + " "); } }

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 spaces.

for (int i=1; i<200; i++){ if ((i%2)==0 && (i%3)==0){ System.out.print(i + " "); } }

Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by spaces.

for (int i=80; i>=20; i--){ if ((i % 2)==0){ System.out.print(i + " "); } }

Write a for loop that prints the integers 0 through 39, separated by spaces.

for (int n = 0; n < 40; n++) System.out.print(n + " ");

You have a variable, n, with a non-negative value, and need to write a loop that will keep print n blank lines. What loop construct should you use?

for loop

Assume the int variables i and result have been declared but not initialized. Write a for loop header, i.e. something of the form for ( . . . ) for the following loop body: result = result * i; When the loop terminates, result should hold the product of the odd numbers between 10 and 20.

for(i=11,result=1;i<20;i+=2)

Assume that the int variables i and j have been declared, and that n has been declared and initialized. Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen.

for(i=1;i<=n;i++) { for(j=1;j<=i;j++) System.out.print('*'); System.out.println(); }

Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already been declared, use a for loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j.

for(j=0;j<n;j++){ System.out.print("*"); }

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;k<=97;k++) System.out.print('*'); System.out.println();

Write a statement that terminates the current loop when the value of the int variables x. and y.are equal

if(x == y){ break; }

Assume the input data is structured as follows: first there is a non-negative integer specifying the number of employee timesheets to be read in. This is followed by data for each of the employees. The first number for each employee is an integer that specifies their pay per hour in cents. Following this are 5 integers, the number of hours they worked on each of the days of the workweek. Given this data, and given that an int variable total has been declared, write a loop and any necessary code that reads the data and stores the total payroll of all employees in total. Note that you will have to add up the numbers worked by each employee and multiply that by that particular employee's pay rate to get the employee's pay for the week-- and sum those values into total. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

int numberOfTimesheets; int centsPerHour = 0; int hoursWorked; total = 0; numberOfTimesheets = stdin.nextInt(); for(int i = 1; i <= numberOfTimesheets; i++){ hoursWorked = 0; centsPerHour = stdin.nextInt(); for (int ii = 1; ii <= 5; ii++){ hoursWorked = hoursWorked + stdin.nextInt(); } total = total + (hoursWorked * centsPerHour); }

Suppose that the code below is the body of some loop. Use a continue statement to make sure that nothing is written to standard out when y is 0.

int x = stdin.nextInt(); int y = stdin.nextInt(); if (y == 0) continue; System.out.println(x / y);

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. Use no variables other than n, k, and total.

k = 0; total=0; while(k <= n ) { total = total + ( k * k * k) ; k++ ; }

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.

k = 1 ; total = 0 ; while ( k <= 50 ) { total = total + ( k * k ) ; k = k + 1 ; }

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();

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; k <= n; k++) total += Math.pow(k,3);

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; for (k=1;k<=50; k++) total+=k*k;

You need to write a loop that reads integers and adds them to a sum as long as they are positive. Once 0 or a negative value is read in your loop terminates. Which is the preferred loop construct to use?

while loop

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.

while(n > 0){ System.out.print("*"); n--; }


संबंधित स्टडी सेट्स

Functional Groups, Assignment, Chemistry B: Functional Groups

View Set

Chapter 5 (1st Test) Pearson Questions

View Set

Managerial Accounting Chapter 7: Incremental Analysis for Short-Term Decision Making

View Set

Chapter 19 Care of Patients with Cardiac Disorder

View Set