Chapter 1a - Variables & Printing
Your First C++ Programβ
Every C++ program needs two things: the iostream header (for input/output) and a main() function.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
| Part | What it does |
|---|---|
#include <iostream> | Loads the input/output library |
using namespace std; | Lets you write cout instead of std::cout |
int main() | Entry point β where the program starts |
cout << | Prints to the screen |
endl | Ends the line (like pressing Enter) |
return 0; | Tells the OS the program finished successfully |
π§ͺ Try it yourself
Challenge: Change the messages to print your own name and a fun fact about yourself.
Variables and Data Typesβ
A variable stores a value in memory. In C++ you must declare the type of data before using a variable.
int age = 17;
double price = 9.99;
string name = "Alice";
bool isStudent = true;
char grade = 'A';
Common Data Typesβ
| Type | What it stores | Example |
|---|---|---|
int | Whole numbers | 42, -5, 0 |
double | Decimal numbers | 3.14, -0.5 |
string | Text (requires <string> or <iostream>) | "Hello" |
bool | True or false | true, false |
char | A single character | 'A', '9' |
π§ͺ Variables in action
Mini quiz β programs and typesβ
Programs and types
1 / 3Where does a C++ program start running?
int main() {
Pick an answer to see if you are right.
Printing with coutβ
Use << to chain multiple values in one cout statement.
string name = "Alice";
int age = 17;
cout << "My name is " << name << " and I am " << age << " years old." << endl;
My name is Alice and I am 17 years old.
You can also use "\n" instead of endl β it is faster for large outputs.
cout << "Line 1\n";
cout << "Line 2\n";
Arithmetic Operatorsβ
int a = 10;
int b = 3;
cout << a + b << endl; // 13 β addition
cout << a - b << endl; // 7 β subtraction
cout << a * b << endl; // 30 β multiplication
cout << a / b << endl; // 3 β integer division (truncates)
cout << a % b << endl; // 1 β modulo (remainder)
10 / 3 in C++ gives 3, not 3.33... because both operands are int. Use double if you need decimals:
double result = 10.0 / 3; // 3.333...
π§ͺ Arithmetic practice
Challenge: leftover applesβ
Finish leftover so it returns how many apples remain after sharing equally (apples % friends).
| apples | friends | Return |
|---|---|---|
24 | 5 | 4 |
10 | 3 | 1 |
User Input with cinβ
cin >> reads a value and stores it in a variable.
π§ͺ Try it: press Run, wait until you see Enter your age:, type a number in the box under the output, then press Enter.
Challenge: Change the program so it also asks for your favorite number and prints both values. After each prompt, type a value and press Enter.
cin >> stops at a space. Use getline(cin, variable) to read a full line:
string fullName;
cout << "Enter your full name: ";
getline(cin, fullName);
cout << "Hello, " << fullName << "!" << endl;
π§ͺ Input example
Exercisesβ
Write a C++ program that asks the user for a temperature in Celsius and prints the Fahrenheit equivalent.
Formula: F = (C Γ 9/5) + 32
Expected output:
Enter temperature in Celsius: 100
100 Celsius = 212 Fahrenheit
π‘ Hint
Use double for the temperature variables so you get decimal precision. Read the Celsius value with cin, compute the formula, then print with cout.