Exercise 3 2 Essay

Submitted By Yves-Petiote
Words: 305
Pages: 2

COP 2210
Exercise 3 More Loops
Name:

1. Consider the following program

import java.util.Scanner;

public class Ex3 { public static void main (String args[]) { int sum=0; for (int i=1; i<=10; i=i+1) sum = sum + i; System.out.println(sum); }
}

Invoke Netbeans and create a project ex3 and enter the above main method.

What is the output produce by the program?
55

2. Now modify the loop to the following:

for (int i=1; i<=10; i=i+1){ if (sum > 28) break; sum=sum +i; } System.out.println(sum);

Compile the program and execute the program.

What is the output produce by the program?
36
What is the purpose of the break statement?
To control the flow of the program.

3. Write a java program that read 2 integers n, m (n<= m) and prints all even numbers between n and m (inclusive.) For example if the input values are 2 and 9 the output must be 2 4 6 8. Or for the input 3 and 12 the program must produce 4 6 8 10 12.

4. Observe that the pattern below has 5 rows where each row i, has i columns containing values 1, 2, … i. Write a program that will print the following pattern:

1
1 2 1 2 3 1 2 3 4 1 2 3 4 5

5. The pattern below has 5 rows where each row i has 5-i+1 columns containing values 1, 2, … i. Write a program that will print the following pattern:

1 2 3 4 5
1 2 3 4 1 2 3
1 2
1

6. Now combine the previous tow program to write a program that prints the