Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Assignment 16 – Loop Tracing

Trace the following program:

Program Output Memory


int d = 10; 8 d = 10
while (d > 5)
6 d=8
{ 4 d=6
d = d - 2; > d=4
System.out.println (d);
}

15 x = 15
int x = 15;
int y = 4;
11 x = 11
do 7 x=7
{ 3 x=3
System.out.println(x); > x = -1
x = x - 4;
} while (x > y);

int input, min; Enter^a^number:^ min = 23


System.out.print ("Enter a number: "); 23
input = sc.nextInt ();
min = 6
45
min = input; 67
95
while (input != -1) 86
{ 6
if (input < min) 93
{ 6
min = input; -1
}
System.out.print ("Enter a number: "); 6
input = sc.nextInt ();
}
System.out.println (min);

Assume the user enters the following in order:


23
45
67
95
86
6
93
6
-1

int x = 3; 6 x=3
int y = 15;
do
9 y = 15
{ 12 x=6
x = x + 3; y = 14
y = y - 1; x=9
System.out.println (x); y = 13
} while (x != y);
x = 12
y = 12
Assignment 17 – For Loops
1. Trace the following programs:
Program Output Memory
Hello i=3
for (int i = 3; i <= 5; i++)
{
Hello i=4
System.out.println(“Hello”); Hello i =5
} Good-bye i =6
System.out.println(“Good-bye”);

-3 count = -3
for (int count = -3; count <= 1; count = count
+ 2)
-1 count = -1
{ 1 count = 1
System.out.println(count); count = 3
}

10 i=7
for (int i = 8; i > 3; i = i – 1)
{
9 i=6
System.out.println(i + 2); 8 i=5
} 7 i=4
6 i=3

int first,second; Input: 11,15


System.out.print(“Enter starting value: “); Output Memory
first = sc.nextInt( ); 11 first = 11
System.out.print(“Enter ending value: “);
second = sc.nextInt( );
* second = 15
12 i = 11
for (int i = first; i <= second; i++) * i = 12
{ 13 i = 13
System.out.println(i ); * i = 14
System.out.println(“*”);
} 14 i = 15
*
15
*

Input: 5, 3
Output Memory
first = 5
(No output, loop second = 3
doesn't execute)
2.
a) Backward1.java: Write a program to output a backwards count by 5's from 100 down to 5.

b) Backward2.java: Modify the program so that before you start the count you can input a number
between 100 and 50 so that the program will stop when the count is less than the number input.

3. Chart.java: Develop a chart, with titles, containing +the squares and cubes of the natural numbers
from 5 to 40.

4. Paper.java: Suppose that a large piece of paper with an area of 1.0m2 and a thickness of 0.090 mm is
cut in half and the two pieces are stacked, one on top of the other. Suppose further that the process of
cutting in half and stacking is repeated over and over again. Write a program to find both the thickness
of the pile and the area of each piece after the procedure has been carried out forty times.

5. TimeTable.java: Write a program that reads a positive integer n and then prints an “n-times table”
containing values up to n X n. For example, if the program reads the values 5, it should print.
5X1=5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
Assume that input is valid.
Assignment 18: More practice on Loops (OPTIONAL BONUS
CHALLENGE)

For each of the following exercises, you may use any loop type which will solve the problem, but try to
choose the loop type will be most appropriate.

1. Alphabets.java Write a program that continuously asks for a letter, and stops when a non-alphabet
character is entered. Then it outputs the number of upper case letters, lower case letters, and vowels
entered.

Enter a letter: A
Enter a letter: a
Enter a letter: z
Enter a letter: y
Enter a letter: j
Enter a letter: I
Enter a letter: &

You have entered 2 upper case letters.


You have entered 4 lower case letters.
You have entered 3 vowels.

HINT: to read a char from the keyboard, you can use commands similar to:
char input;
input = sc.nextLine().charAt(0);
This will let the user type in a string, but take the first character in that string.

You may assume that the user will always type in a single character.
2. EvenNumbers.java Write a program that continuously asks for an integer, and stops when a
non-positive integer is entered. Then it outputs the number of even numbers entered.

Enter an integer: 2
Enter an integer: 9
Enter an integer: 102
Enter an integer: 2321
Enter an integer: 91
Enter an integer: 0
You have entered 2 even numbers.

Assignment 19: Nested Loops


1. Trace the following programs.

Program Memory Output


final int SIZE = 5; SIZE = 5 7
for (int i = 1; i < SIZE; i++) {
for (int j = SIZE; j >= i; j--) {
i and j are loop 6
System.out.println(i * 2 + j); variables. 5
} 4
} 9
8
7
11
10
13
12
15
14
17
16
>
for (int j = 22; j <= 25; j++) { j and k are loop 22
for (int k = 7; k <= 10; k++) {
if (j % 2 == k % 2) {
variables. 7
System.out.println(j); 22
} else { 8
System.out.println(k); 22
} 9
}
System.out.println(); 22
} 10

23
7
23
8
23
9
23
10
>
24
7
24
8
24
9
24
10
>
25
7
25
8
25
9
25
10
>
for (int j = 8; j > 2; j-=2) { j and k are loop 8:
System.out.println(j + “:”);
for (int k = 2; k <= j; k++) {
variables. 810
System.out.print(j+k); 6:
} 68
System.out.println(); 4:
} 46
>

2. Rectange.java Write a program that draws a rectangle with stars, given the number of rows and
columns.

Sample Input Sample Output


Enter # of rows: 5 ****
Enter # of columns: 4 ****
****
****
****

3. TableOfNumbers.java Write a program that will prompt the user for two numbers x & y then output a
table of numbers with x rows and each of the rows lists numbers from 1 to y, separated by a space.

Sample Input Sample Output


Enter x: 5 1 2 3 4 5 6
Enter y: 6 1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6

4. TriangleOfNumbers.java Write a program using nested loops that prints the following pattern.
1
12
123
1234
12345

5. PythagoreanTriples.java Three positive integers a, b, and c with a < b < c form a Pythagorean triplet
if a2 + b2 = c2. For example 3, 4, 5 form a Pythagorean triplet since 32 + 42 = 52. Write a program that first
prompts the user for a positive integer and then finds and prints all Pythagorean triples whose largest
member is less than or equal to that integer.
3. NumberSum.java Write a program that adds up integers that the user enters. First the program asks
how many numbers will be added up. Then the program prompts the user for each number. Finally it
prints the sum.

How many integers will be added?


5
Enter an integer:
3
Enter an integer:
4
Enter an integer:
-4
Enter an integer:
-3
Enter an integer:
7

The sum is 7

Be careful not to add the number of integers (in the example, 5) into the sum.

4. Limit.java Write a program that computes the following sum:

sum = 1.0/1 + 1.0/2 + 1.0/3 + 1.0/4 + 1.0/5 + .... + 1.0/N

N is an integer limit that the user enters.

Enter N
4

Sum is: 2.08333333333

5. SquareCube.java Write a program that adds up the squares and adds up the cubes of integers from
1 to N, where N is entered by the user:

Upper Limit:
5
The sum of Squares is 55
The sum of Cubes is 225

Power.java Write a program that computes where is a floating point number and is a positive
integer. The program informs the user that must be positive if the user enters a negative value. Of
course,
XN = X * X * X * ... * X
--------------------
N times

The user dialog will look something like this:

Enter X
1.3
Enter N
5

1.3 raised to the power 5 is: 3.71293

-------

Enter X
5.6
Enter N
-3

N must be a positive integer.

6. Factors.java Write a program that asks the user for an integer and then prints out all its factors. For
example, when the user enters 150, the program should print
1
2
3
5
6
10
15
25
30
50
75
150

You might also like