basis Syntax using C++
basis Syntax using C++
int main() {
/* This is a
multi-line comment */
// Declaring variables
cout << "Name: " << name << "\n"; // \n for newline
cout << "Age: " << age << "\t"; // \t for tab space
}
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 << "Hello, " << name << "! You are " << age << " years old." << endl;
return 0;
}
Practical 2: Simple Calculator
Task:
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;
return 0;
}
Practical 3: Even or Odd Checker
Task:
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;
if (num % 2 == 0) {
cout << num << " is an even number." << endl;
} else {
cout << num << " is an odd number." << endl;
}
return 0;
}