Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

COUNTING AND LOOPING: The program in LoveCS.java prints \"I love Computer Scienc

ID: 3621306 • Letter: C

Question

COUNTING AND LOOPING:
The program in LoveCS.java prints "I love Computer Science!!" 10 times. Copy it to your directory and compile and run it to see how it works. Then modify it as follows:

1. Instead of using constant LIMIT, ask the user how many times the message should be printed. You will need to declare the variable to store the user's response and use that variable to control the loop. (Remember that all caps is used only for constants!)
2. Number each line in the output, and add a message at the end of the loop that says how many times the message was printed. So if the user enters 3, your program should print this:

1 I love Computer Science!!
2 I love Computer Science!!
3 I love Computer Science!!
Printed this message 3 times.

3. If the message is printed N times, compute and print the sum of the numbers from 1 to N. So for the example above, the last line would now readL
Printed this message 3 times. The sum of the numbers from 1 to 3 is 6.

Note that you will need to add a variable to hold the sum.

Explanation / Answer

import java.io.BufferedReader; import java.io.InputStreamReader; public class CountingLooping { public static void main(String[] args) { int n = 0; int sum = 0; BufferedReader bin = new BufferedReader( new InputStreamReader(System.in)); try { System.out.print("Number of times to print: "); n = Integer.parseInt(bin.readLine()); } catch (Exception e) { e.printStackTrace(); } for (int i = 1; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote