C++ Programming Labs

Master C++ programming through hands-on coding challenges. Build real programs and develop strong programming foundations.

C++ Fundamentals - Module 1

Write actual C++ code to solve each challenge. Your code is validated for correctness.

Lab 1: Hello World & Basic Output
Beginner
Coding Challenge
Your Task: Create your first C++ program that displays a welcome message and introduces you to console output.

Detailed Requirements:
1. Include the iostream header - This is essential for input/output operations in C++. The #include <iostream> directive tells the compiler to include the iostream library which provides cout for output and cin for input.

2. Use the std namespace - Add using namespace std; after your include statement. This allows you to use cout instead of std::cout. Without this, you'd need to prefix all standard library functions with std::.

3. Create the main function - Every C++ program needs a int main() function as its entry point. This is where program execution begins. The function returns an integer (typically 0 for success).

4. Print "Hello, World!" - Use cout << "Hello, World!" << endl; to display the message. The << operator is the insertion operator that sends data to the output stream. endl creates a new line and flushes the buffer.

5. Print your name - Add another cout statement: cout << "My name is [YourName]" << endl;

6. Return 0 - End your main function with return 0; to indicate successful program execution. Non-zero return values typically indicate errors.

Key Concepts:
cout stands for "character output" - it writes to the standard output (usually your screen)
endl stands for "end line" - it moves the cursor to a new line
• You can chain multiple << operators: cout << "Hello " << "World" << endl;

Expected Output:
Hello, World! My name is [YourName]

Requirements Checklist

Include the iostream header (#include <iostream>)
Use the std namespace (using namespace std;)
Create int main() function with curly braces
Print "Hello, World!" using cout
Print your name on a separate line
Return 0 at the end of main()
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Include header: #include <iostream>
• Namespace shortcut: using namespace std;
• Main function: int main() { ... return 0; }
• Output text: cout << "Your text" << endl;
• The semicolon (;) ends every statement in C++
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 2: Variables & Data Types
Beginner
Coding Challenge
Your Task: Create a program that demonstrates C++ fundamental data types by storing and displaying personal profile information.

Detailed Requirements:
1. Declare a string variable "name" - In C++, strings require the <string> header OR you can use it with iostream. Syntax: string name = "John Doe"; Strings store text/characters and must be enclosed in double quotes.

2. Declare an int variable "age" - The int type stores whole numbers (integers) without decimal points. Range: approximately -2.1 billion to +2.1 billion. Syntax: int age = 25;

3. Declare a double variable "height" - The double type stores decimal numbers (floating-point) with double precision (about 15-16 significant digits). Syntax: double height = 1.75; Use this for measurements, prices, or any value needing decimals.

4. Declare a char variable "grade" - The char type stores a single character. IMPORTANT: Characters use SINGLE quotes, not double quotes! Syntax: char grade = 'A';

5. Declare a bool variable "isStudent" - The bool type stores only two values: true or false. Used for conditions and flags. Syntax: bool isStudent = true;

6. Print all variables with labels - Use cout to display each variable with a descriptive label. You can combine text and variables using the << operator: cout << "Name: " << name << endl;

Key Concepts:
• C++ is a statically-typed language - you must declare the type of each variable
• Variable names are case-sensitive (age ≠ Age ≠ AGE)
• Initialize variables when declaring them to avoid undefined behavior
• Use meaningful variable names that describe their purpose

Expected Output:
Name: John Doe Age: 25 Height: 1.75 Grade: A Is Student: 1
Note: Boolean values print as 1 (true) or 0 (false) by default. To print "true"/"false", you can use boolalpha manipulator.

Requirements Checklist

Declare a string variable "name" with a value
Declare an int variable "age" with a value
Declare a double variable "height" with a decimal value
Declare a char variable "grade" with a letter
Declare a bool variable "isStudent"
Print all variables using cout with labels
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• String: string name = "Alice"; (double quotes)
• Integer: int age = 30;
• Double: double height = 1.65;
• Character: char grade = 'B'; (single quotes!)
• Boolean: bool isStudent = false;
• Print: cout << "Label: " << variable << endl;
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 3: Arithmetic Operations
Beginner
Coding Challenge
Your Task: Build a calculator program that demonstrates all C++ arithmetic operators.

Detailed Requirements:
1. Declare two integer variables - Create int a = 20; and int b = 7; These will be used to demonstrate all arithmetic operations.

2. Perform Addition (+) - Calculate int sum = a + b; and print the result. The + operator adds two numbers together.

3. Perform Subtraction (-) - Calculate int difference = a - b; and print. The - operator subtracts the right operand from the left.

4. Perform Multiplication (*) - Calculate int product = a * b; and print. The * operator multiplies two numbers.

5. Perform Division (/) - Calculate int quotient = a / b; and print. IMPORTANT: Integer division truncates (removes) the decimal part! 20/7 = 2, not 2.857. For decimal results, use double.

6. Perform Modulus (%) - Calculate int remainder = a % b; and print. The modulus operator returns the REMAINDER after division. 20 % 7 = 6 because 20 ÷ 7 = 2 remainder 6.

Key Concepts:
• Integer division truncates: 5 / 2 = 2 (not 2.5)
• For decimal division, use double: 5.0 / 2.0 = 2.5
• Modulus only works with integers, not doubles
• Operator precedence: * / % happen before + - (use parentheses to control order)

Bonus Challenge: Add a double division to show the decimal result!
double decimalDiv = (double)a / b; - This casts 'a' to double before division.

Expected Output:
a = 20, b = 7 Addition: 20 + 7 = 27 Subtraction: 20 - 7 = 13 Multiplication: 20 * 7 = 140 Division: 20 / 7 = 2 Modulus: 20 % 7 = 6

Requirements Checklist

Declare two int variables (a and b) with values
Calculate and print addition (a + b)
Calculate and print subtraction (a - b)
Calculate and print multiplication (a * b)
Calculate and print division (a / b)
Calculate and print modulus (a % b)
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Declare variables: int a = 20; int b = 7;
• Addition: cout << a + b << endl;
• Subtraction: cout << a - b << endl;
• Multiplication: cout << a * b << endl;
• Division (integer): cout << a / b << endl; → Result: 2
• Modulus: cout << a % b << endl; → Result: 6
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below