✍️ Practice (While)
1. Spot the Problems
This program prints the 7 multiplication table:
Console.WriteLine("7 x 1 = 7");
Console.WriteLine("7 x 2 = 14");
Console.WriteLine("7 x 3 = 21");
Console.WriteLine("7 x 4 = 27");
Console.WriteLine("7 × 5 = 35");
Console.WriteLine("7 x 6 = 42");
- Find the two mistakes.
- How many lines would you need to change to print the 8 times table instead?
- How would you let the user choose which time table to print?
👀Hints
- Check every result. Then compare the lines character by character, because two characters can look the same without actually being the same.
- On each line, count how many numbers depend on which table you're printing.
- Look at what changes from one line to the next and what stays the same.
💡Solution
7 x 4should equal28, not27. Line 5 uses the×symbol instead of the letterx.- All 6 lines, and each one has 2 numbers to change, so 12 edits in total.
- You can't with this approach, because the results would need to be calculated rather than typed out.
2. Times Table
Rewrite the program using a while loop. It should:
- ask the user which times table to print,
- print that table from 1 to 10.
💡Solution
Console.Write("Enter the times table you want to make: ");
int table = int.Parse(Console.ReadLine());
int i = 1;
while (i <= 10)
{
Console.WriteLine($"{table} x {i} = {table * i}");
i = i + 1;
}
3. Bart's Blackboard
Like Bart Simpson writing lines on the chalkboard, write a program that prints a message as many times as the user asks.

👀Hint
Pseudocode
- Ask the user for a message.
- Ask how many times to repeat it.
- Create a counter starting at
0. - While the counter is less than the number entered, print the message and increment the counter.
💡Solution
Console.Write("Enter a message: ");
string message = Console.ReadLine();
Console.Write("How many times? ");
int repeatCount = int.Parse(Console.ReadLine()); // assumes a valid whole number
int counter = 0;
while (counter < repeatCount)
{
Console.WriteLine(message);
counter++;
Thread.Sleep(1000); // Program will sleep for 1 second (1000 milliseconds)
}
Optional: Add
Thread.Sleep(1000);inside the loop for vibes
4. Fibonacci
Write a C# program to find and print the first 10 Fibonacci numbers using a while loop.
Each Fibonacci number is the sum of the two before it:
0, 1, 1, 2, 3, 5, 8, 13, ...
👀Hint
Pseudocode
- Create two variables to hold the first two Fibonacci numbers:
first = 0andsecond = 1. - Create a counter starting at
0. - While the counter is less than
10:- Print
first. - Calculate the next number:
next = first + second. - Shift the numbers along:
firstbecomessecond, andsecondbecomesnext. - Increment the counter.
- Print
💡Solution
int first = 0;
int second = 1;
int counter = 0;
while (counter < 10)
{
Console.Write($"{first}, ");
int next = first + second;
first = second;
second = next;
counter++;
}