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

I have a program to code for my computer science class, and Im not sure how...
I have a program to code for my computer science class, and Im not sure how to do it. If someone can explain it step by step I would appreciate it. Problem Description: Define the GeometricObject2D class that contains the properties color and filled and their appropriate getter and setter methods. This class also contains the dateCreated property and the getDateCreated() and toString() methods. The toString() method returns a string representation of the object. Define the Rectangle2D class that extends...
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...
COMPUTER SCIENCE- FLOATING POINT REPRESENTATION: Hello, I completed a program for floating point representation (it can...
COMPUTER SCIENCE- FLOATING POINT REPRESENTATION: Hello, I completed a program for floating point representation (it can add and multiply floating point values in IEEE format). I already completed it, but I came back with 3 small errors. Can someone please fix them? I posted them at the bottom, here is the code: __________________________________________________________________________________________________________________________________ fp.java: // fp class public class fp {    // add function public int add(int a, int b) { FPNumber fa = new FPNumber(a); FPNumber fb =...
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...
Copy the program Stats.java to your computer and implement the TBI (To Be Implemented) stats() and...
Copy the program Stats.java to your computer and implement the TBI (To Be Implemented) stats() and checkIfSorted() methods following the instructions given in their method comment blocks. ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Stats.java import java.util.*; /* * This Java application calculates some statistics * given an array of integers. * * @creator gdt * @created 02017.12.15 * @updated 02019.01.21 morphed the assignment */ interface StatsConstants { // used by print() to format output... String PRINT_BEGIN = "["; String PRINT_END = "]"; String PRINT_SEPARATOR =...
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...
home / study / engineering / computer science / questions and answers / i have a...
home / study / engineering / computer science / questions and answers / i have a c++ question, its already posted on here ... Question: I have a c++ question, its already posted on here ... Bookmark I have a c++ question, its already posted on here but the answer given is way too complex and i dont understand it... its only the first month of c++ so please use the basic code... thank you. Assume that ax^2 + bx...
I am unsure as to why I cannot solve my Computer Science question I was just...
I am unsure as to why I cannot solve my Computer Science question I was just wondering if someone could show me exactly what they would do the code is in C++: write a program that display the following manual for user to chose among calculations Please select one of the following: addition subtraction multiplication division exit The program will then read in the user entry. when the user chose "1", it will ask the user "Please enter two numbers...
I am working on a project for my Computer Science course. I am trying to create...
I am working on a project for my Computer Science course. I am trying to create a Battleship game where a user names two coordinates on a grid and is then told whether this results in a "Hit" or a "Miss". Once the ship has been hit a certain number of times (based on the size of the ship) the ship is sunk. I have written all the code but it now fails to execute when I try to run...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT