ProgramsC++

Basic Programs

Basic C++ programs for cout, cin, types, and simple math. First week, walked line by line, with the input traps and integer-division surprises that catch beginners.

14 programs · open one, then the next

Start with Hello World
  1. 01Hello WorldBeginnerWrite your first C++ program and see why each of its four lines matters: #include, using namespace std, main, cout, and return 0. Made for day-one students.Open example →
  2. 02Display Your NameBeginnerStore your name in a C++ string variable and print it with cout and endl. Covers #include <string>, chaining <<, and how a string differs from a char array.Open example →
  3. 03User InputBeginnerRead a name and an age from the keyboard with cin in C++. See why cin >> stops at the first space, when to use getline, and how a bad number breaks the stream.Open example →
  4. 04Sum of Two NumbersBeginnerAdd two integers read with cin and print the result in C++. A first-week lesson on input, the + operator, failed reads, and silent int overflow near 2 billion.Open example →
  5. 05Swap Two NumbersBeginnerSwap two integers in C++ with a temporary variable and print both values before and after. Shows why the temp is needed and what std::swap and XOR tricks do.Open example →
  6. 06Size of int, float, double, charBeginnerPrint the size in bytes of char, int, float, double, long, and long long with sizeof in C++. Learn which sizes are guaranteed and which change between compilers.Open example →
  7. 07Float MultiplicationBeginnerMultiply two decimal numbers in C++ using float, then print the product to two places with fixed and setprecision. Explains float precision and <iomanip>.Open example →
  8. 08ASCII Value of a CharacterBeginnerFind the ASCII code of any character in C++ by casting a char to int. Covers why A is 65, why the digit 0 is 48, and how ch - '0' turns a digit into a number.Open example →
  9. 09Convert Fahrenheit to CelsiusBeginnerConvert a Fahrenheit temperature to Celsius in C++ with C = (F - 32) * 5 / 9. Shows why 5 / 9 is 0 in integer math and why the parentheses must stay.Open example →
  10. 10Calculate Simple InterestBeginnerCalculate simple interest and the total amount in C++ using P * R * T / 100. Explains why the rate is a percent, why float is used, and how to check your answer.Open example →
  11. 11Calculate Compound Interest with powBeginnerCompute yearly compound interest in C++ with pow from <cmath>. A beginner lesson on A = P(1 + R/100)^T and why the ^ operator is XOR, not a power.Open example →
  12. 12Area and Perimeter of RectangleBeginnerFind the area and perimeter of a rectangle from length and width in C++. Practice two formulas, float input, and why 2 * (l + w) needs its parentheses.Open example →
  13. 13Find Quotient and RemainderBeginnerFind the quotient and remainder of two integers in C++ with / and %. Learn integer division, how negatives round toward zero, and why dividing by zero crashes.Open example →
  14. 14Add Two NumbersBeginnerRead two integers and print their sum in C++, declaring the result right where it is computed. A short practice drill on cin, + and the type limits of int.Open example →