What is the output of the following loop? Comment on the code. (This is not the same as the previous exercise.)
Want to see the full answer?
Check out a sample textbook solutionChapter 3 Solutions
Absolute Java (6th Edition)
Additional Engineering Textbook Solutions
C Programming Language
Starting Out With Visual Basic (7th Edition)
Computer Systems: A Programmer's Perspective (3rd Edition)
Starting Out with Python (3rd Edition)
C How to Program (8th Edition)
- What are the run times of the following for loops? for(int i =0; i < n1; ++i) { for(int j =0; j < n2; ++j) { for(int k =0; k < n3; ++k) { sum += i + j + k; } } }arrow_forwardWhat is the result of the following code? int xNum T 0; int yNum = 0; do { xNum = xNum+yNum; yNum += 1; cout << "xNum: } while (xNum > 2); << xNum; O a.xNum: 2 is printed O b. no output, the loop is never executed O c. xNum: 0 is printed O d. endless looparrow_forwardCan you explain how the value of low is 8 and the value of high is 9 after executing the loop? I'm supposed to use the loop invariant to get the values of low, high, and n.arrow_forward
- Please use java Using two nested loops, initialize the board so that zeros and ones’ alternate, as on a checkerboard:0 1 0 1 0 1 0 11 0 1 0 1 0 1 00 1 0 1 0 1 0 11 0 1 0 1 0 1 00 1 0 1 0 1 0 1…1 0 1 0 1 0 1 0Hint: check whether i + j is evenarrow_forwardWrite a program that reads in five integers using loops and then determines and printsthe largest and smallest integers in the group. Use only the techniques you have learnt so far, andmake sure that the program only uses three variables. (Hint: use two of the variables to hold thecurrent largest and smallest integers.)arrow_forwardWhat is the exact output of the following nested loop? for (int row = 2; row < 10; row += 4) { int column =1; while (column <= 4) { System.out.print(row - column + " " ); column += 2; } System.out.println(); } 1 -1 5 3 1 -1 5 3 All of these are correct. 1 -1 5 3arrow_forward
- Describe the running time in terms of the variable n. it would be better to say That loop will run 2*n + 1 times instead of That loop will run O(n) times. public int mysterious(int n, int sum) { int i = 0; while (i < n) { i = i + 1; for (int k = 0; k < 25; k++) { sum = sum + i; } } return sum; }arrow_forwardRewrite the following as a for loop. int i = 0, value = 0;while (i <= 20){if (i % 2 == 0 && i <= 10)value = value + i * i;else if (i % 2 == 0 && i > 10) value = value + i;elsevalue = value - i;i = i + 1;}cout << "value = " << value << endl;What is the output of this loop?arrow_forwardwrite a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out, ona line by itself, the sum of all even integers read. a while loop and python is required to do the questionarrow_forward
- My teacher wants me to put a while loop into my program, where do I put it for the best outcome? public static void main(String[] args) { int min = 19; int num1=0, num2=0, num3=0; int tol1=0, tol2=0, tol3=0; int max = 50; int col[]=new int[49]; for(int i = 0; i < 49; i++){ int ran = (int)(Math.random() * (max - min + 1) + min); if( ran <= 20 && ran < 30){ num1 += 1; tol1 += ran; } if(ran <= 30 && ran < 40){ num2 += 1; tol2 += ran; } if(ran <= 40 && ran < 50){ num3 += 1; tol3 += ran; } col[i]= ran; } for(int i = 0; i < 49; i++ ){ if(i == 7 || i == 14 || i == 21 || i == 28 || i == 35 ||i == 42){ System.out.println("\n"); } System.out.printf(("%7d"),+ col[i]); } System.out.printf("\n"); System.out.printf("Found "+ num1 +" number(s) in the 20s "+ "totaling "+ tol1);…arrow_forwardWhat is the exact output of the following nested for loop? for (int row = 1; row < 10; row += 4) { for (int column =1; column <= 5; column += 2) { System.out.print(row * column + " " ); } System.out.println(); } 1 3 5 5 15 25 9 27 45 All of the are correct. 1 5 9 3 15 25 5 25 45 1 5 9arrow_forwardQuestion 2: Write a program to read student ID and check whether it is odd/even number. Output a 4X4 badge if the student ID is even number and output a 3X3 badge if it is odd number. The badge is built up by the (height X width) of asterisks. (Use for-loop to draw the asterisks, do not hard-coded)arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning