While Loops
1. For each of the following while loops, state how many times the loop will execute its body: “zero,”
“infinity,” and “unknown”. Also, what is the output of the code in each case?
int x = 1;
while (x < 100) {
printf(" %d", x);
x += 10;
}
int max = 10;
while (max < 10) {
printf("count down: %d\n", max);
max--;
}
int x = 250;
while (x % 3 != 0) {
printf(“%d\n”, x);
}
int x = 2;
while (x < 200) {
printf("%d ", x);
x *=x;
}
int x = 100;
while (x > 0) {
printf(“%d”, x / 10);
x /= 2;
}
2. Convert each of the following for loops into an equivalent while loop:
for (int n = 1; n <= max; n++) {
printf(“%d\n”, n);
}
int total = 25;
for (int number = 1; number <= (total / 2); number++) {
total = total – number;
printf("%d %d\n", total, number);
}
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
for (int k = 1; k <= 4; k++) {
printf("*");
}
printf("!");
}
printf(“\n”);
}
int number = 4;
for (int count = 1; count <= number; count++) {
printf(“%d\n”, number);
number /= 2;
}
3. Determine the output of the following code for five different values of variable x: 1, 6, 19, 39, 74.
int y = 1;
int z = 0;
while (2 * y <= x) {
y *= 2;
z++;
}
printf("%d %d\n", y, z);
4. Determine the output of the following code for five different values of variable x: 19, 42, 48, 40, 64.
int y = 0;
while (x % 2 == 0) {
y++;
x /= 2;
}
printf("%d %d \n", x, y);
5. Given the following code, what range of values can each variable (a, b, c, d, and e) have?
int a = rand() % 100;
int b = rand() % 20 + 50;
int c = rand() % (20 + 50);
int d = rand() % 100 – 20;
int e = rand() % 10 * 4;
6. Write code that generates a random odd integer (not divisible by 2) between 50 and 99 inclusive.
7. For each of the following do/while loops, state how many times the loop will execute its body: “zero,” “infinity,” and
“unknown”. Also, what is the output of the code in each case?
int x = 1;
do {
printf("%d ", x);
x += 10;
} while (x < 100);
int max = 10;
do {
printf("count down: %d", max);
max––;
} while (max < 10);
int x = 250;
do {
printf(“%d \n”, x);
} while (x % 3 != 0);
int x = 100;
do {
printf(“%d \n”, x);
x /= 2;
} while (x % 2 == 0);
int x = 2;
do {
printf(“%d ”, x);
x *= x;
} while (x < 200);
int x = 100;
do {
printf(“%d \n”, x/10);
x /= 2;
} while (x > 0);
8. Write a do/while loop that repeatedly prints a certain message until the user tells the program to stop. The do/while
is appropriate because the message should always be printed at least one time, even if the user types n after the first
message appears. The message to be printed is as follows:
She sells seashells by the seashore.
Do you want to hear it again? y
She sells seashells by the seashore.
Do you want to hear it again? y
She sells seashells by the seashore.
Do you want to hear it again? n
9. Write a program that takes an integer input from the user and prints the number of digits in the number that have the
value 0. For example, in response to the input 5024036, the program should print 2, and to input 743 should print 0. (a
do/while loop is suggested to keep your program executes repeatedly in your solution.)
10. Write a do/while loop that repeatedly prints random numbers between 0 and 1000 until a number above 900 is
printed. At least one line of output should always be printed, even if the first random number is above 900. Here is a
sample execution:
Random number: 235
Random number: 15
Random number: 810
Random number: 147
Random number: 915
11. Write a sentinel loop that repeatedly prompts the user to enter a number and, once the number –1 is typed, displays
the maximum and minimum numbers that the user entered. Here is a sample dialogue:
Type a number (or –1 to stop): 5
Type a number (or –1 to stop): 2
Type a number (or –1 to stop): 17
Type a number (or –1 to stop): 8
Type a number (or –1 to stop): –1
Maximum was 17
Minimum was 2
12. Write a sentinel loop that repeatedly prompts the user to enter a number and, once the number –1 is typed, displays
the sum of even and the sum of odd numbers that the user entered. Here is a sample dialogue:
This program reads numbers and displays sum of Even numbers and sum of Odd
numbers.
Type a number (or –1 to stop): 2
Type a number (or –1 to stop): 19
Type a number (or –1 to stop): 7
Type a number (or –1 to stop): 4
Type a number (or –1 to stop): -1
Sum of Even numbers : 26
Sum of Odd numbers : 6