#include <iostream> // Include the standard input-output library
using namespace std; // Use the standard namespace
int main() {
// This is a single-line comment
/* This is a
multi-line comment */
// Declaring variables
int age = 25; // Integer variable
double height = 5.9; // Floating-point variable
char grade = 'A'; // Character variable
string name = "John Doe"; // String variable
// Using escape sequences
cout << "Name: " << name << "\n"; // \n for newline
cout << "Age: " << age << "\t"; // \t for tab space
cout << "Height: " << height << "\n";
cout << "Grade: " << grade << "\n";
return 0; // Return 0 to indicate successful execution
}
Practical 1: Basic Input and Output
Objective: Learn how to use cin and cout for basic input and output operations.
Task:
Write a C++ program that asks the user for their name and age.
Print a greeting message using their input.
Example Output:
yaml
CopyEdit
Enter your name: Alice
Enter your age: 20
Hello, Alice! You are 20 years old.
Code Template:
cpp
CopyEdit
#include <iostream>
using namespace std;
int main() {
string name;
int age;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
return 0;
}
Practical 2: Simple Calculator
Objective: Learn to use arithmetic operators.
Task:
Write a program that takes two numbers from the user.
Perform addition, subtraction, multiplication, and division.
Example Output:
yaml
CopyEdit
Enter first number: 10
Enter second number: 5
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Code Template:
cpp
CopyEdit
#include <iostream>
using namespace std;
int main() {
double num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Addition: " << num1 + num2 << endl;
cout << "Subtraction: " << num1 - num2 << endl;
cout << "Multiplication: " << num1 * num2 << endl;
cout << "Division: " << num1 / num2 << endl;
return 0;
}
Practical 3: Even or Odd Checker
Objective: Learn to use conditional statements.
Task:
Write a program that asks the user for a number.
Determine whether the number is even or odd.
Example Output:
csharp
CopyEdit
Enter a number: 7
7 is an odd number.
Code Template:
cpp
CopyEdit
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0) {
cout << num << " is an even number." << endl;
} else {
cout << num << " is an odd number." << endl;
}
return 0;
}