Concept explainers
What output will be produced by the following code, when embedded in a complete
int x = 2;
cout << “Start\n”;
if (x <= 3)
if (x ! = 0)
cout << “Hello from the second if.\n”;
else
cout << “Hello from the else.\n”;
cout << “End\n”;
cout << “Start again\n”;
if (x > 3)
if (x ! = 0)
cout << “Hello from the second if.\n”;
else
cout << “Hello from the else.\n”;
cout << “End again\n”;
Want to see the full answer?
Check out a sample textbook solutionChapter 3 Solutions
Problem Solving with C++, Student Value Edition plus MyProgrammingLab with Pearson eText -- Access Card Package (9th Edition)
Additional Engineering Textbook Solutions
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
C++ How to Program (10th Edition)
Absolute Java (6th Edition)
Java How To Program (Early Objects)
Digital Fundamentals (11th Edition)
- 4. A) What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z); B) Suppose a = 1. What is the value of a after the following code executes? Show the procedure to reach your answer if(a > 0) { switch(a) { case 1: a = a + 3; case 3: a++; case 6: a = a * 2; break; case 8: a--; break; default a++; } else a = a + 2; C) What would be the value of x after the following statements were executed? int x = 10; switch (x) { case 10: x += 15; case 12: x -= 5; break; default: x *= 3; }arrow_forwardThe program down below has a code for a program to play a guessing game with the user. The program should randomly generate an integer between 1 and 10, then ask the user to try to guess the number. If the user guesses incorrectly, the program should ask them to try again until the guess is correct; when the guess is correct, the program should print a congratulatory message. 1. Complete the given program below so that it plays the game as described above. // ************************************************************ // Guess.java // // Play a game where the user guesses a number from 1 to 10 // // ************************************************************ import java.util.Scanner; import java.util.Random; public class Guess { public static void main(String[] args) { int numToGuess; //Number the user tries to guess int guess; //The user's guess Scanner scan = new Scanner(System.in); Random generator = new Random(); //randomly generate the number to guess //print message asking…arrow_forwardWhat would be the output of the following program? Question options: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 None of these optionsarrow_forward
- What is the output of the following (when embedded in a complete program)? if (12<12) cout << “Never”; else cout << “Always”;arrow_forwardFor this question, you will write a program that prompts the user to enter an integer value between 1 and 1000 (exclusive). Your program should then print out a sequence of numbers between the given value and 1 (inclusive) following the pattern below:For a given number ai, If ai is even, then ai+1 = floor(ai / 2) If ai is odd, then ai+1 = 3ai + 1 Your program should stop printing numbers once the sequence reaches the value of 1. If the user enters a number that is not between 1 and 1000 (exclusive), print "Invalid input." and prompt the user for a number again. This sequence is referred to as the Collatz sequence. The Collatz Conjecture states that this sequence will eventually reach the value of 1 for any starting number, a fact that has not yet been proven by modern mathematics! Note: floor(x) means that x should be rounded down to the nearest integer.arrow_forwardWhat is the output of the following code segment? int num= 20, y = 5, x; cin>>X; if (x >= 2*num) y+=1; else if (x >= num) y+=2; if (xarrow_forward
- What is the output of the following code? x = 2;y = -6;if ( x + y > 0 and x*y > 0 thenprint "yes";else if ( x = 0 )print "no";elseprint "maybe";endif;arrow_forwardWrite two scnr.nextInt statements to get input values into birthMonth and birthYear. Then write a statement to output the month, a slash, and the year. End with newline.The program will be tested with inputs 1 2000 and then with inputs 5 1950. Ex: If the input is 1 2000, the output is:1/2000Note: The input values come from user input, so be sure to use scnr.nextInt statements, as in birthMonth = scnr.nextInt();, to get those input values (and don't assign values directly, as in birthMonth = 1). What would this code be in Java? Below is what they have started for us. import java.util.Scanner; public class InputExample {public static void main(String [] args) {Scanner scnr = new Scanner(System.in);int birthMonth;int birthYear;}}arrow_forwardWrite two scnr.nextInt statements to get input values into birthMonth and birthYear. Then write a statement to output the month, a slash, and the year. End with newline.The program will be tested with inputs 1 2000 and then with inputs 5 1950. Ex: If the input is 1 2000, the output is:1/2000Note: The input values come from user input, so be sure to use scnr.nextInt statements, as in birthMonth = scnr.nextInt();, to get those input values (and don't assign values directly, as in birthMonth = 1).arrow_forward
- What will be displayed as a result of executing the following code? int x = 5, y = 16; x *= 5; y += 4; System.out.println("num1 = " + x + ", num2 = " + y); Select one: a. x+y b. num1 = 5, num2 = 4 c. num1+num2 d. num1 = 25, num2 = 20arrow_forwardWrite a program that prompts the user to enter an even integer in the twenties. Using logical operators and just one if expression, the program should simply respond with "Good input" or "Bad input". program3_3.pyWrite a second version of program3_2 that is not restricted to one if expression. This version should identify good inputs and all possible problems for bad inputs, these being:Not even, too low. Even, too low. Even, too high. Not even, too high. Not even, in range. Good input.arrow_forwardQuestion 5 What is the output of the following code segment? int num cin>>x; = 20, 5, X; if (x >= 2*num) y+=1; else if (x >= num) y+=2; if (xarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education