Skip to main content

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;
}
PartWhat 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
endlEnds 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​

TypeWhat it storesExample
intWhole numbers42, -5, 0
doubleDecimal numbers3.14, -0.5
stringText (requires <string> or <iostream>)"Hello"
boolTrue or falsetrue, false
charA single character'A', '9'
πŸ§ͺ Variables in action

Mini quiz β€” programs and types​

Quiz

Programs and types

1 / 3

Where 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)
Integer Division

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).

applesfriendsReturn
2454
1031

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.

Reading strings with spaces

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​

Activity: Temperature Converter

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.

πŸ“’ Solution