Possible Answer:
#include <stdio.h>
int main(void)
{
for (int i = 1; i <= 50; i++)
{
if (i % 2 == 0)
{
("%d\n", i);
printf}
}
return 0;
}
Another Possible Answer:
#include <stdio.h>
int main(void)
{
for (int i = 2; i <= 50; i += 2)
{
("%d\n", i);
printf}
return 0;
}
Possible Answer:
#include <stdio.h>
int main(void)
{
for (int i = 0; i < 33; i++)
{
("%d\n", (i + 1) * 3);
printf}
return 0;
}
Another Possible Answer:
#include <stdio.h>
int main(void)
{
for (int i = 1; i <= 33; i++)
{
("%d\n", i * 3);
printf}
return 0;
}
Sample Answer:
#include <stdio.h>
int main(void)
{
for (int i = 1; i <= 100; i++)
{
// Skip over all numbers evenly divisible by 5
if (i % 5 == 0)
{
continue;
}
("%d\n", i);
printf}
return 0;
}
Possible Answer:
#include <stdio.h>
int main(void)
{
for (int i = 8; i <= 148; i = i + 8)
{
// If i isn't evenly divisible by 3, print it
if (i % 3 != 0)
{
("%d\n", i);
printf}
}
return 0;
}
Another Possible Answer:
#include <stdio.h>
int main(void)
{
int i = 8;
while (i <= 148)
{
// If i isn't evenly divisible by 3, print it
if (i % 3 != 0)
{
("%d\n", i);
printf}
= i + 8;
i }
return 0;
}
void the_coolest_loop(void)
{
int i;
for (i = 0; i < 10; i--)
{
("i is %d!\n", i);
printf}
}
Answer: This is an infinite loop! i
never goes past 10 (since we’re always decreasing) so the array goes on
forever.
void print_sums(void)
{
int sum;
for (int j = 0; j != 10; j++)
{
sum = sum + j;
}
printf("Sum is %d\n", sum);
printf("Resetting...\n");
sum = 0;
for (int j = 0; j != 10; j++)
{
sum = sum + j;
}
printf("Sum is now %d\n", sum);
}
Answer: Both loops are doing the same thing, but
sum
is not initialized (only declared) in line 3, so it has
bleh inside of it. Because of this only the second print is
going to be correct saying that “Sum is now 45”.
int number = 5;
while (number < 500)
{
("number = %d\n", number);
printf++;
number}
Answer:
for (int number = 5; number < 500; number++)
{
("number = %d\n", number);
printf}
int items;
int item_cost = 5;
int remaining_dollars = 50;
("You have $%d. Let's see how many items you can buy!\n", remaining_dollars);
printf
for (items = 0; remaining_dollars >= item_cost; items++)
{
-= item_cost;
remaining_dollars }
("You bought a total of %d items with your money!\n", items); printf
Answer:
int items = 0;
int item_cost = 5;
int remaining_dollars = 50;
("You have $%d. Let's see how many items you can buy!\n", remaining_dollars);
printf
while (remaining_dollars >= item_cost)
{
-= item_cost;
remaining_dollars ++;
items}
("You bought a total of %d items with your money!\n", items); printf
int x;
// Loop 1
= 0;
x while (x < 10)
{
("%d\n", x);
printf++;
x}
// Loop 2
= 0;
x do
{
("%d\n", x);
printf++;
x} while (x < 10);
Answer: This is a trick question! There is no difference in output between these two loops. Both of them will print the following:
0
1
2
3
4
5
6
7
8
9
(For another challenge, in what cases would a while loop differ from a do-while loop? Are they interchangeable, or is one sometimes more appropriate than the other?)
sum > 200
.Answer:
int sum = 0;
for (int i = 0; i < n; i++)
{
if (sum > 200)
{
break;
}
+= i;
sum }
#include <stdio.h>
int main(void)
{
("We are starting the loop!\n");
printf
for (int i = 0; i < 5; i++)
{
if (i == 3)
{
return 0;
}
("i = %d\n", i);
printf}
("We are ending the loop!\n");
printf
return 0;
}
Answer:
We are starting the loop!
i = 0
i = 1
i = 2
In this program, the first printf()
statement is
executed. Then the for loop begins, printing the statements
i = 0
, i = 1
, and i = 2
. Once
i
becomes 3, the condition in the if statement becomes
true. Because of this, return 0;
is executed, which ends
the main()
function. Consequently, the program finishes
without any additional printf()
statements.
loopy_loop()
if executed?int loopy_loop(void)
{
int x = 0;
while (x < 10)
{
("x = %d\n", x);
printf
if (x == 5)
{
return 2;
}
+= 2;
x }
return 5;
}
Answer: 5
Notice that as this while loop is running, x
will be set
to every even number between 0 and 9. Because of this, the conditional
statement x == 5
will never be true in the loop, and the
return 2;
statement will never be executed. Once the loop
finishes, loopy_loop()
will return 5.
5
10
4
-9
0
The sum is: 10
Sample Answer:
#include <stdio.h>
int main(void)
{
int sum = 0;
int n;
do
{
("%d", &n);
scanf+= n;
sum } while (n != 0);
("The sum is: %d\n", sum);
printf
return 0;
}
n
by n
“addition table.” An addition table is like a multiplication table,
except the row and column numbers are added instead of being multiplied.
For example, the following is a 5x5 addition table.0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
Answer:
#include <stdio.h>
int main(void)
{
// n can be set to any value, or even be collected through user input.
int n = 5;
// This uses two loops, the outer loop iterates row-by-row, and the
// inner loop iterates across an individual row.
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
// Here, we just print the sum of the current row and column number.
// The 5 in %5d helps align all of the numbers.
("%5d", row + col);
printf}
// At the end of each row, print a new line, visually starting the next row.
("\n");
printf}
return 0;
}
n
. Assume that n
is
greater than 2 and is odd. As an example, the following is a diamond
with a height of 5. #
# #
# #
# #
#
Sample Answer:
#include <stdio.h>
int main(void)
{
// Break down into 3 sections per row: the space outside the diamond, the #'s
// making up the diamond, and the space inside the diamond
int n = 5;
int outer_spaces, inner_spaces;
for (int row = 0; row < n; row++)
{
// Calculate number of outer spaces
if (row <= n / 2)
= (n / 2) - row;
outer_spaces else
= row - (n / 2);
outer_spaces
// Print outer spaces
for (int i = 0; i < outer_spaces; i++)
{
(" ");
printf}
// Print left side of diamond
("#");
printf
// Calculate number of inner spaces
if (row <= n / 2)
= (2 * (row - 1)) + 1;
inner_spaces else
= (2 * (n - row - 2)) + 1;
inner_spaces
// Print inner spaces
for (int i = 0; i < inner_spaces; i++)
{
(" ");
printf}
// Print right side of diamond (if not the top or bottom)
if (row != 0 && row != n - 1)
{
("#");
printf}
// End the row
("\n");
printf}
return 0;
}