In: Computer Science
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 //
|
// **************************************************************** |
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++;
}
}
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!)
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.
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.