LOOP PRACTICE

advertisement
LOOP PRACTICE
WARM-UP:
Start by writing the control statement (test) for each.
Then write the priming step (start).
Next write the body (action) and don’t forget restart.
1. Input numbers until 0 is entered and print whether odd or even.
start
test
action
restart
2. Input temperatures until 999 and print whether above or below freezing.
start
test
action
restart
3. Input names until XXX and print Hello and the corresponding name.
start
test
action
restart
4. Ask user if they want to enter a temperature and print whether above or
below freezing.
start
test
action
restart
5. Print Hello 15 times.
start
test
action
restart
6. Input temperatures until 999 and count number above and below freezing.
start
test
action
restart
Exercises
7. Write the statements that allow you to input a series of numbers and
print whether it is positive or negative. Entering 0 causes the program to
quit.
8. Input state codes until XX and print out the corresponding state.
9. Write the statements to input numeric exam scores until negative and
output the appropriate letter grade.
10. Change the first problem to ask the user if they want to execute the
program.
11. Continuously Display a menu of operator symbols and include an option to
quit.
12. Write a loop that will continuously input and print the names of contest
participants, until QUIT is entered in place of the name.
13. Write code that will input a number and print the number, the square of
the number, and the cube of the number. Continue the operation until
999 is entered.
14. Write a program that will request the length and width of a rectangle
until 0 is entered for either the length or the width. Print the area and
perimeter for each set of inputs.
15. Write code to input test scores from the user and counts the number of
As and Bs and prints both counts to the screen.
16. Input all grades until 999 is entered and find the highest.
17. Write a program that reads in tests scores (until 999) and calculates the
average. Continue the operation until 999 is entered.
Implement using a while loop and a for loop:
20. Write a program to print your name 5 times.
21. Write a program to print the numbers from 0 to 10
22. Write a program to print the numbers from 10 to 0.
23. Write a program to print the following numbers:
5 10 15 20 25 30 35
24. Write a program to input a number and print your name that many
times.
Trace:
25. What is the output for the following loop?
int num;
num
output
num = 1;
while (num < 5)
{
num = num + 1;
System.out.println(num);
}
26. What is the output for the following loop?
int number = 2;
number
done output
boolean done = FALSE;
while (!done)
{
number = number * 2;
if (number > 12)
done = TRUE;
System.out.println(number);
}
27. Given the following program segment:
int j;
j = console.nextInt();
while (j > 0)
{
System.out.print( "*");
j--;
}
System.out.println( "!");
a. What will print if 2 is input for j?
b. What will print if 0 is input for j?
28. What is the output, assume the following input: 25 10 6 -1.
sum = 0;
number = console.nextInt();;
while (number != -1)
{
number = console.nextInt();;
sum = sum + number;
}
System.out.println(sum);
32. What is the output for the following loop?
int num;
num = 1;
while (num < 5)
{
num = num + 1;
System.out.println(num);
}
33. What is the output for the following loop?
int number = 2;
boolean done = FALSE;
while (!done)
{
number = number * 2;
if (number > 12)
done = TRUE;
cout << number << endl;
}
34. What is the value of loopCount after control exits the following loop?
loopCount = 0;
while (loopCount < 145)
{
alpha = alpha + 7;
loopCount++;
}
35. Given the following program segment:
What will print if 2 is input for j?
What will print if 0 is input for j?
int j;
System.out.println( j);
j = console.nextInt();
while (j > 0)
{
System.out.print( "**");
j--;
}
System.out.println("!");
37. Give the output, assuming the following numbers are input: 10 5 6 -1.
For the following code, trace all values of number and sum (All variables are
int.)
sum = 0;
number = console.nextInt();
while (number != -1)
{
number = console.nextInt();
sum = sum + number;
}
System.out.println( sum );
FOR Loops
38. Make the following numbers print to the screen 1 4 7 10 13 16
39. Make the following numbers print to the screen 20 15 10 5
40. Print out 500 asterisks with no spaces or newlines.
41. Ask the user to enter in a number. Print out that many *’s on the screen without spaces or
newlines.
Example,
Please enter a number
7
*******
42. Input 15 temperatures and compute the average.
43. State what output, if any, results in each of the following statements:
a. for ( i = 0; i <= 1; i++)
System.out.println("*");
System.out.println(i);
b. for ( i = 2; i >= 1; i++)
System.out.println( "*");
System.out.println(i);
c. for ( i = 1; i <= 1; i--)
System.out.println( "*");
System.out.println();
d. for ( i = 12; i >= 9; i--)
System.out.println("*");
System.out.println(i);
e. for ( i = 0; i <= 5; i++)
System.out.println( "*");
System.out.println(i);
f. for ( i = 1; i <= 5; i++)
{
System.out.println( "*");
i = i + 1;
}
System.out.println(i);
NESTED LOOPS
38. Write a program that prints out the sequence of all the hour and minute
combinations in a day, starting with 00:00:00 and ending at 12:00:00.
39. What is the output of the following:
for (i = 1; i <= 2; i++)
{
for (j = 3; j < 4; j++)
System.out.println( i + " " + j + " ") ;
}
40. for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 2; j++)
{
System.out.println("six");
}
}
41. for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 10; j++)
{
System.out.print((i * j) + " ");
}
System.out.println(); // to end the line
}
42. for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
43. What is the output of the following:
for (i = 0; i < 2; i++)
{
for (j = 3; j <= 4; j++)
System.out.println( i + " " + j );
}
Do…while
18. What is the output in each case?
a.
x = 5; y = 50;
do
x = x + 10;
while (x < y);
System.out.println(x + " " + y);
b.
x = 5; y = 80;
do
{
x = x + 5;
y = y - 10;
} while (x < y);
System.out.println(x + " " + y);
19. What is the output for the following program?
x = 5; y = 30;
do
x = x + 10;
while (x < y);
System.out.println(x + " " + y);
20.
Write code that continuously prints the following menu:
1. Convert Fahrenheit to Celsius
2.
Convert Celsius to Fahrenheit
3.
Quit
When the user selects each option, the program does the appropriate
conversion.
C = (5/9) (F-32)
F = (9/5 * C) + 32
Be sure and handle invalid entries. The program repeats until the user
chooses to quit.
FILE
44. Write the statements to input numeric exam scores from a file and output
the appropriate letter grade.
45. Read all temperatures from a file and compute the average.
46. Write a program that reads in integers and then counts and prints out the
number of positive integers and negative integers. If a value is zero, it
should not be counted. The process should continue until end-of-file occurs.
47. What is the output of the following if the file contains: 2 3 4 5 6 7
int sum, num;
sum = 0;
num = infile.nextInt();
while (inFile.hasNextInt())
{
if (num % 2 == 0)
sum = sum + num;
num = infile.nextInt();
}
System.out.println(sum);
Download
Related flashcards
Number theory

27 Cards

Special functions

14 Cards

Create flashcards