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.
#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.
A sum program always prints 12 and passes the sample 7 5. Which next test most directly exposes the mistake?
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.
#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
Mark complete when you can explain it yourself.