Question

In: Computer Science

Counting and Looping The program in LoveCS.java prints “I love Computer Science!!” 10 times. Copy it...

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:

// ****************************************************************

// LoveCS.java //

  • //    Use a while loop to print many messages declaring your
    
  • //    passion for computer science
    

// ****************************************************************

public class LoveCS
{
public static void main(String[] args)

{

final int LIMIT = 10;

int count = 1;

while (count <= LIMIT){

}

System.out.println("I love Computer Science!!"); count++;

}

}

  1. Instead of using constant LIMIT, ask the user how many times the message should be printed. You will need to declare a 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 read:

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.

Solutions

Expert Solution

import java.util.Scanner;
public class LoveCS
{
    public static void main(String[] args)
    {

        //to read the input from user we need to create Scanner class object
        Scanner scn=new Scanner(System.in);
                //declare variables
        int i,N,sum=0;
                //display message
        System.out.println("Please tell how many times the message should be printed ? ");
        //nextInt() method will be used to take an integer number from the user
        N=scn.nextInt();
                
                //initialize count by 1 
        int count = 1;
        
                //loop from 1 to N
                while (count <= N)
        {
            //printing message line by line
                        System.out.print(count+" I love Computer Science!!"); 
                        //increment count by 1
            count++;
        
        }
        System.out.print("Printed this message "+(count-1) +" times.");
        //to find the sum of all numbers from 1 to N and store the sum in sum variable
        for(i=1;i<=N;i++)
        {
            sum=sum+i;
        }
                //printing the sum
        System.out.print("The sum of the numbers from 1 to "+N+" is "+sum);
    }
}

Sample Input and Output of above Java code:

Sample #1

Please tell how many times the message should be printed ?
3
1 I love Computer Science!!2 I love Computer Science!!3 I love Computer Science!!Printed this message 3 times.The sum of the numbers from 1 to 3 is 6

Sample #2


Please tell how many times the message should be printed ?
5
1 I love Computer Science!!2 I love Computer Science!!3 I love Computer Science!!4 I love Computer Science!!5 I love Computer Science!!Printed this message 5 times.The sum of the numbers from 1 to 5 is 15

Sample #3
Please tell how many times the message should be printed ?
10
1 I love Computer Science!!2 I love Computer Science!!3 I love Computer Science!!4 I love Computer Science!!5 I love Computer Science!!6 I love Computer Science!!7 I love Computer Science!!8 I love Computer Science!!9 I love Computer Science!!10 I love Computer Science!!Printed this message 10 times.The sum of the numbers from 1 to 10 is 55

Sample #4

Please tell how many times the message should be printed ?
15
1 I love Computer Science!!2 I love Computer Science!!3 I love Computer Science!!4 I love Computer Science!!5 I love Computer Science!!6 I love Computer Science!!7 I love Computer Science!!8 I love Computer Science!!9 I love Computer Science!!10 I love Computer Science!!11 I love Computer Science!!12 I love Computer Science!!13 I love Computer Science!!14 I love Computer Science!!15 I love Computer Science!!Printed this message 15 times.The sum of the numbers from 1 to 15 is 120

Sample #5

Please tell how many times the message should be printed ?
1
1 I love Computer Science!!Printed this message 1 times.The sum of the numbers from 1 to 1 is 1

Code Explainations:

Here We have used the basic concepts of loops,like how to loop for a given number of times.

further for taking of a user input we need to create the object of Scanner class which is present in java.util package, so we have to first import java.util.Scanner and then we can use it.

Then we have used nextInt() method of Scanner class to take an integer value from user and store this value in N.

Now we have run a while loop from 1 to N and print the message along with the line number N number of times on screen.

Then we have printed how many times the message got printed on screen.

and finally we have found the sum of all numbers from 1 to N and store the result in sum varible and finally we have printed it.

Note : I have used for loop to find the sum. We can also use the while loop there.

Also if you want to print the message each time on a new line then simply you have to write System.out.println() instead of System.out.print() function.

Here I am providing the code Screenshot for easy and quick reference.


Related Solutions

ID Documents 1 I love data mining 2 The seven dwarves love mining 3 Data science...
ID Documents 1 I love data mining 2 The seven dwarves love mining 3 Data science is a hot new career 4 I don't love my major or career Use the corpus of documents shown in the above table to answer the quiz questions below. What is the inverse document frequency (IDF) of the term "love"? (Round your answer to 2 decimal places). What is the TF-IDF value (importance) of the term "data" to document 1? (Round your answer to...
python Write a program that prints your name 100 times to the screen. Write a function...
python Write a program that prints your name 100 times to the screen. Write a function that takes a string s and an integer n as parameters and prints the string s a total of n times (once per line). Write a for loop that prints all the integers from 3141 to 5926, skipping every other one. Write a for loop that prints every 5th integer from 5926 down to 3141. Write a program (using a for loop) to print...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
You are one of the top students in your university’s computer science program of 200 students....
You are one of the top students in your university’s computer science program of 200 students. You are surprised when you are met after class by two representatives from a federal intelligence agency. Over dinner, they talk to you about the increasing threat of cyberterrorist attacks launched on the United States by foreign countries and the need to counter those attacks. They offer you a position on the agency’s supersecret cyber terrorism unit, at a starting salary 50 percent higher...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the bubble sort algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here       ...
i want to write research paper in the field of computer science but cant decide the...
i want to write research paper in the field of computer science but cant decide the topic for that plz help me.
I'm in a computer science course and I have an assignment that asks me to implement...
I'm in a computer science course and I have an assignment that asks me to implement a CPU scheduler in Java. Here are some details for the task: The program should ask for the number of processes. Then the program will ask for burst and arrival times for each process. Using that information, the program should schedule the processes using the following scheduling algorithms: First Come First Serve (FCFS) Shortest Jobs First (SJF) Shortest Remaining Time First (SRTF) The output...
Program 5: Decimal to Binary In Computer Science we often work with different number systems to...
Program 5: Decimal to Binary In Computer Science we often work with different number systems to represent data. The most commonly used number system (which we refer to as "decimal") is a "base 10" number system, which means that we use the values 0-9 to represent all numbers. The binary number system is a "base 2" number system, which means that we use the values 0 and 1 to represent all numbers. We refer to each value in a binary...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT