Exercise 4: Control Flow Challenge

Problem Statement

Write a Rust program that demonstrates control flow by implementing:

  1. A FizzBuzz program that prints numbers from 1 to N, but:
  2. For multiples of 3, print "Fizz" instead of the number
  3. For multiples of 5, print "Buzz" instead of the number
  4. For multiples of both 3 and 5, print "FizzBuzz"
  5. A menu-driven calculator that:
  6. Prompts the user to select an operation (add, subtract, multiply, divide)
  7. Asks for two numbers
  8. Performs the selected operation and displays the result
  9. Allows the user to perform multiple calculations until they choose to exit

Learning Objectives

  • Practice using if-else statements, match expressions, and loops
  • Learn how to handle user input with control flow
  • Implement common programming patterns using Rust's control flow constructs
  • Understand how to create a menu-driven program with multiple options

Starter Code

use std::io;

fn main() {
    // Part 1: FizzBuzz Implementation
    println!("=== FizzBuzz Challenge ===");
    // TODO: Implement the FizzBuzz algorithm for numbers 1 to 20

    // Part 2: Menu-driven Calculator
    println!("\n=== Calculator ===");
    // TODO: Implement a menu-driven calculator with a loop that:
    // 1. Shows operation options
    // 2. Gets the user's choice
    // 3. Gets two numbers from the user
    // 4. Performs the calculation and shows the result
    // 5. Asks if the user wants to perform another calculation
}

How to Run Your Code

  1. First, modify the starter code in 04_control_flow_challenge_starter.rs according to the requirements
  2. Run your code from the bootcamp root directory with: cargo run --bin module1_04

Expected Output

For the FizzBuzz portion:

=== FizzBuzz Challenge ===
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...

For the calculator portion:

=== Calculator ===
Choose an operation:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 1
Enter first number: 10
Enter second number: 5
Result: 10 + 5 = 15
Do you want to perform another calculation? (y/n): y
...

Tips

  • For FizzBuzz, consider using a for loop with range
  • Check divisibility using the modulo operator %
  • Use match expressions for the menu selection in the calculator part
  • Remember to handle potential errors when parsing user input
  • Consider using a loop like loop with a break condition for the calculator's main operation