Question
In this exercise, you use what you have learned whle loops. Study the following code, and then answer the Questions 1-5. import java.util.*; public class
In this exercise, you use what you have learned whle loops. Study the following code, and then answer the Questions 1-5.
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner scan = new Scanner (System.in);
int no, counter;
System.out.print ("How many pages do you want to print? ");
no = scan.nextInt();
counter = 1;
while (counter <= no) ;
{
System.out.print (counter);
counter++;
}
}
}
Answer the following questions.
1- What is the output if the user enters 6 ? ____________
2. What is the problem with this code, and how can you fix it? ____________
3. Assume you fix the problem, if the user enters 50 as the number of pages to print, what is the value of counter when the loop exits? ______________
4. Assume you fix the problem, if the user enters 0 as the number of pages to print, how many pages will print? ______________
5. Assume you fix the problem discussed in Q2, and then delete the curly braces. Now, what is the output if the user enters 5 as the number of pages to print? ______________
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started