Exercise 4: Control Flow Challenge
Problem Statement
Write a Rust program that demonstrates control flow by implementing:
- A FizzBuzz program that prints numbers from 1 to N, but:
- For multiples of 3, print "Fizz" instead of the number
- For multiples of 5, print "Buzz" instead of the number
- For multiples of both 3 and 5, print "FizzBuzz"
- A menu-driven calculator that:
- Prompts the user to select an operation (add, subtract, multiply, divide)
- Asks for two numbers
- Performs the selected operation and displays the result
- 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
- First, modify the starter code in
04_control_flow_challenge_starter.rsaccording to the requirements - 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
forloop withrange - Check divisibility using the modulo operator
% - Use
matchexpressions for the menu selection in the calculator part - Remember to handle potential errors when parsing user input
- Consider using a loop like
loopwith a break condition for the calculator's main operation