My Programming Lab

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

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

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

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

for loop

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

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


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

CIPS L4M1 - Question & Answer Past exam questions

View Set

Quiz #5, Esposito et al, Chapter 5

View Set

Operating Systems - Assignment 3 - Process Description and Control

View Set

Society and Culture: Demographics

View Set

Body Cavities and Organs located within

View Set