Exercise 4.5
- The fragment that follows is supposed to print a table of squares of the integers from 1 to 100:
int i;
for (i = 1; i < 100; i++);
println(i + " " + i*i);
What would it actually print?
- What value(s) of the variable response will stop the following loops?
- while (response <= 'a' && response >= 'z' )...
- while (response >= 'A' || response <= 'E' )...
- What is wrong with the following fragment?
char response;
char transCode;
do
{
transCode = getChar("Enter transaction code");
println("Code entered: " + transCode);
response = getChar("Is this correct? (Y/N)");
} while (response != 'Y' || response != 'N');
- Modify the fragment so that it behaves in a more appropriate
way.
- How many times will the following loop be executed? Justify your
answer.
int n = 40;
while (n > 0);
{
println(n);
n /= 2;
}
- What will be printed by each fragment?
-
for (i = 0; i <= 5; i++)
println(i*i);
n = 3;
do
println(++n);
while (n < 8);
for (i = 25; i > 0; i -= 4)
println(i);
for (i = j = 1; i < 5; i++)
{
j *= i;
println(j);
}
- Describe in a few words what would be stored in the variable value
after execution of each fragment.
for (i = value = 0; i <= 10; i++)
value += i;
n = getlnt("");
value = 0;
while (n % 2 == 0)
{
value++;
n /= 2;
}
n = getInt("");
value = 0;
do
{
value += n % 10;
n /= 10;
}
while (n > 0);
- A prime number is a positive integer that has exactly two distinct
divisors - one and the number itself. For example, 17 is a prime
number because it is divisible only by 1 and 17. Write a program
that reads an integer and then prints a message stating whether or
not it is a prime number.
- Write a program that prompts the user for a natural number and,
once one has been supplied, finds and prints all of its exact divisors.
- Write a program that could be used to play a simple game. The
program should first ask one user to supply a positive integer less
than 1000. Once this user has provided an appropriate value,
the program should than ask a second user to guess the number
that was given by the first user. After each incorrect guess, the
program should tell this user whether the guess was too low or
too high and ask for another guess. This should continue until
the second user has found the correct number. The program
should then print the number of guesses that the second user
needed to determine the number.
- What guessing strategy will minimize the expected number of
incorrect guesses?
- Let n be a positive integer consisting of the digits dk ... d1d0. Write a
program that reads a value of n and then prints its digits in a column,
starting with d0. For example, if n = 3467, then the program should
print:
- Rewrite the program in the previous question so that it prints the
digits of n in the opposite order, starting with dk and working down
to d0.
- State the output from the following program:
for (int i = 3; i <= 5; i++)
for (int j = i; j >= 1; j--)
{
if (j % 2 == 0)
println();
print(j);
}
|
|
- State the output from the following program:
for (int i = 3; i > 0; i--)
{
for (int j = 0; j <= i; j++)
print(i + j);
println();
}
|
|
|
|