C++ Programming Labs

Master control flow and decision making in C++. Learn conditionals, loops, and functions.

Control Flow & Functions - Module 2

Learn to control program execution with conditionals, loops, and reusable functions.

Lab 4: If-Else Conditionals
Beginner
Coding Challenge
Your Task: Build a grade calculator that converts a numeric score (0-100) into a letter grade using if-else statements.

Detailed Requirements:
1. Declare an int variable "score" - Initialize it with a test value between 0-100. Example: int score = 85; Try different values to test all grade paths (95, 82, 71, 65, 45).

2. Declare a char variable "grade" - This will store the letter grade result. Example: char grade; Note: We declare without initializing since we'll assign it in the if-else.

3. Implement the grading logic using if-else chain:
   • if (score >= 90) → Assign grade = 'A' (Excellent)
   • else if (score >= 80) → Assign grade = 'B' (Good)
   • else if (score >= 70) → Assign grade = 'C' (Average)
   • else if (score >= 60) → Assign grade = 'D' (Below Average)
   • else → Assign grade = 'F' (Failing)

Key Concepts:
Order matters! Check conditions from highest to lowest. If you check >= 60 first, a score of 95 would get a 'D'!
Comparison operators: >= (greater or equal), > (greater), <= (less or equal), < (less), == (equal), != (not equal)
else if only runs if all previous conditions were false
else is the catch-all for when no conditions match

4. Add pass/fail status: Use a separate if-else to print whether the student passed (score >= 60) or failed.

Expected Output:
Score: 85 Grade: B Status: PASSED

Requirements Checklist

Declare int variable "score" with a test value (0-100)
Use if statement to check for grade A (score >= 90)
Include else if statements for grades B, C, D
Include else block for grade F (failing)
Print the score and calculated grade
Print pass/fail status (60+ is passing)
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Declare score: int score = 85;
• Declare grade: char grade;
• If syntax: if (score >= 90) { grade = 'A'; }
• Else-if: else if (score >= 80) { grade = 'B'; }
• Characters use single quotes: 'A' not "A"
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 5: For Loops
Beginner
Coding Challenge
Your Task: Implement the classic FizzBuzz challenge - a common programming interview question that tests your understanding of loops, conditionals, and the modulo operator.

Detailed Requirements:
1. Create a for loop from 1 to 20:
for (int i = 1; i <= 20; i++) { ... }
   • int i = 1 - initialization (loop counter starts at 1)
   • i <= 20 - condition (continue while i is 20 or less)
   • i++ - increment (add 1 to i after each iteration)

2. For each number, apply these rules IN THIS EXACT ORDER:
   • If divisible by BOTH 3 AND 5 → print "FizzBuzz"
   • Else if divisible by 3 only → print "Fizz"
   • Else if divisible by 5 only → print "Buzz"
   • Else → print the number itself

Key Concept - The Modulo Operator (%):
The % operator returns the remainder after division.
15 % 3 = 0 → 15 is divisible by 3 (no remainder)
15 % 5 = 0 → 15 is divisible by 5 (no remainder)
7 % 3 = 1 → 7 is NOT divisible by 3 (remainder is 1)
• If i % 3 == 0 evaluates to true, then i IS divisible by 3

⚠️ Critical Order: You MUST check "FizzBuzz" (both 3 AND 5) FIRST! Why? Numbers like 15 are divisible by both. If you check "Fizz" (divisible by 3) first, 15 would print "Fizz" instead of "FizzBuzz".

Expected Output (first 15 numbers):
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz

Requirements Checklist

Create a for loop that iterates from 1 to 20
Check FizzBuzz FIRST (divisible by both 3 AND 5)
Check for Fizz (divisible by 3 only)
Check for Buzz (divisible by 5 only)
Print the number if none of the above conditions match
Output matches the expected FizzBuzz pattern
Output
// Expected: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz
Hints & Tips
• For loop: for (int i = 1; i <= 20; i++) { }
• Check both: if (i % 3 == 0 && i % 5 == 0)
• Check divisible by 3: else if (i % 3 == 0)
• Check divisible by 5: else if (i % 5 == 0)
• Print number: else { cout << i << endl; }
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 6: Functions
Intermediate
Coding Challenge
Your Task: Create reusable functions to perform mathematical calculations. Functions allow you to write code once and use it multiple times.

Detailed Requirements:
1. Create an "add" function:
int add(int a, int b) { return a + b; }
   • int - return type (the function returns an integer)
   • add - function name
   • (int a, int b) - parameters (inputs the function accepts)
   • return a + b; - returns the sum back to the caller

2. Create a "multiply" function:
int multiply(int a, int b) { return a * b; }
Same structure as add, but returns the product instead.

3. Create an "isEven" function:
bool isEven(int num) { return num % 2 == 0; }
   • Returns bool (true or false)
   • Takes one parameter
   • Uses modulo: if num % 2 equals 0, the number is even

4. Call each function from main():
   • Call add(5, 3) and print the result
   • Call multiply(4, 7) and print the result
   • Call isEven(10) and isEven(7) to test both cases

Key Concepts:
• Functions must be DECLARED before they are called (put them above main or use prototypes)
return sends a value back and exits the function immediately
• Parameters are like local variables that receive values when the function is called
• Functions make code reusable, readable, and easier to debug

Expected Output:
5 + 3 = 8 4 * 7 = 28 Is 10 even? 1 Is 7 even? 0

Requirements Checklist

Create an "add" function that takes two int parameters and returns their sum
Create a "multiply" function that takes two int parameters and returns their product
Create an "isEven" function that returns bool (true if even, false if odd)
Call add() function and print the result
Call multiply() function and print the result
Call isEven() function with both even and odd numbers
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Add function: int add(int a, int b) { return a + b; }
• Multiply: int multiply(int a, int b) { return a * b; }
• isEven: bool isEven(int num) { return num % 2 == 0; }
• Call function: add(5, 3) returns 8
• Functions must be defined BEFORE main() or use forward declarations
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below