Learning pathOriginal reference
Lesson 1 of 12

From a problem to a program

Identify the input, output, and constraints, then translate a small plan into C++.

Understand before you type

A problem gives you information and asks for a result. Identify four things: the task, the input format, the exact output format, and the constraints. For two pencil counts a and b, the plan is simply: read both counts, add them, print the sum.

An algorithm must work for every allowed input. Always printing the sample answer is not an algorithm for the general problem. Solve a tiny example by hand, describe the steps in ordinary language, and test the smallest and largest allowed values.

Your first complete C++23 program

#include <iostream> makes the input and output tools available. Execution begins in main. Braces group the instructions, ordinary statements end in semicolons, and C++ names are case-sensitive. return 0 reports successful completion; it does not print a zero.

For input 7 5, this program prints 12. std::cin reads the next values in order, skipping whitespace for numeric input. std::cout inserts the result into the output stream. The newline starts the next output line.

C++23
#include <iostream>

int main() {
    int a = 0;
    int b = 0;
    std::cin >> a >> b;
    std::cout << a + b << '\n';
    return 0;
}

Compile, predict, then run

Save the program as main.cpp. A GNU compiler command is g++ -std=c++23 -Wall -Wextra -Wpedantic main.cpp -o main. Compile again after changing the source. On Linux or macOS, run the resulting program with ./main.

Before running, predict the output. For a judge, print only the requested answer: no input prompts or extra explanations. Output adds no spaces automatically; std::cout << a << b prints 49 when a is 4 and b is 9. Insert a space character explicitly when needed.

Pause & predict

A sum program always prints 12 and passes the sample 7 5. Which next test most directly exposes the mistake?

Choose an answer
Try it yourself

Turn the idea into code

Read the length and width of a rectangle, both from 1 to 1000. Print its area and perimeter, separated by a space. For 5 3, the output is 15 16.

Give me a hint

Write two formulas: area = length × width; perimeter = 2 × (length + width).

Show the explained solution

Read both dimensions, calculate each expression, and print the two results with an explicit separator. Parentheses ensure that both dimensions are doubled for the perimeter.

C++23
#include <iostream>

int main() {
    long long length = 0, width = 0;
    std::cin >> length >> width;
    std::cout << length * width << ' ' << 2 * (length + width) << '\n';
    return 0;
}

Your notes

Ready for the next idea?

Mark complete when you can explain it yourself.

From a problem to a programIdentify the input, output, and constraints, then translate a small plan into C++.Variables, types, and remembered valuesChoose a representation, initialize it, and understand what assignment really changes.Arithmetic without surprisesPredict division and remainder, widen before multiplication, and format decimal answers.Conditions and logical decisionsBuild exclusive categories, combine comparisons, and protect operations with short-circuit logic.Optional: other ways to chooseRecognize switch for exact choices and the conditional operator for choosing a value.For loops: make repetition preciseTrace initialization, condition, body, and update—and avoid an extra iteration.While, do-while, and stopping rulesChoose a loop from its stopping condition and read the exact input format.Break, continue, and early answersDistinguish leaving a loop from skipping one iteration, without losing required input.Five patterns worth understandingUse sums, counts, best-so-far candidates, products, and flags to process a stream.Nested loops: rows and columnsTranslate a shape into two loop roles and count the total work.Take a number apart, one digit at a timeUse remainder and division to process decimal digits, including zero.Multiple cases and a debugging mindsetCombine the patterns, isolate each case, and test the boundaries that can break your idea.Arrays: give every value a placeUnderstand indices, initialization, capacity, active length, and the first safe array loop.Scan, count, search, and compareDistinguish values from positions and preserve the exact tie rule a problem asks for.Change a sequence without losing itUpdate, copy, compare, reverse, and inspect neighbours using the correct bounds.Matrices: think in two coordinatesTraverse tables, total rows and columns, inspect diagonals, and protect grid neighbours.Characters become stringsSeparate characters from numbers, then safely inspect and edit managed text.Read the text the problem actually givesChoose words or full lines, preserve empty cases, and reset only the state that needs it.Scan text: count, filter, transformBuild a result from matching characters without losing leading zeros or skipping shifted text.Join, compare, and find textLearn content comparison, lexicographical order, substring counts, and the not-found sentinel.