Basic Programs
Basic C++ programs for cout, cin, types, and simple math. First week, walked line by line.
14 programs · open one, then the next
Start with Hello World- 01Hello WorldBeginnerPrints "Hello World" in C++ with cout. Covers #include, main, and return 0.Open example →cpp// This line allows us to use input and output (like printing on the screen) #include <iostream> using namespace std;
- 02Display Your NameBeginnerStore a name in a C++ string and print it with cout.Open example →cpp#include <iostream> #include <string> using namespace std;
- 03User InputBeginnerRead a name and age in C++ with cin. cin >> stops at the first space.Open example →cpp#include <iostream> #include <string> using namespace std;
- 04Sum of Two NumbersBeginnerAdd two integers in C++ and print the sum. Watch int overflow and failed cin.Open example →cpp#include <iostream> using namespace std; int main() {
- 05Swap Two NumbersBeginnerSwap two numbers in C++ using a temporary variable.Open example →cpp#include <iostream> using namespace std; int main() {
- 06Size of int, float, double, charBeginnerPrint sizeof char, int, float, double, and long in C++. long is 4 or 8 bytes depending on the OS.Open example →cpp#include <iostream> using namespace std; int main() {
- 07Float MultiplicationBeginnerMultiply two floats in C++ and print two decimal places with setprecision. Needs iomanip.Open example →cpp#include <iostream> #include <iomanip> using namespace std;
- 08ASCII Value of a CharacterBeginnerPrint the ASCII value of a character in C++ by casting char to int. 'A' is 65.Open example →cpp#include <iostream> using namespace std; int main() {
- 09Convert Fahrenheit to CelsiusBeginnerConvert Fahrenheit to Celsius in C++. 5/9 as integers is 0 — use 5.0/9.0.Open example →cpp#include <iostream> #include <iomanip> using namespace std;
- 10Calculate Simple InterestBeginnerSimple interest in C++: P × R × T / 100. Rate 5 means 5%, not 0.05.Open example →cpp#include <iostream> #include <iomanip> using namespace std;
- 11Calculate Compound InterestBeginnerCompound interest in C++ with pow. ^ is XOR, not exponent.Open example →cpp#include <iostream> #include <iomanip> #include <cmath> using namespace std;
- 12Area and Perimeter of RectangleBeginnerArea and perimeter of a rectangle in C++ from length and width.Open example →cpp#include <iostream> using namespace std; int main() {
- 13Find Quotient and RemainderBeginnerQuotient and remainder in C++ using / and %. 7/2 is 3.Open example →cpp#include <iostream> using namespace std; int main() {
- 14Add Two NumbersBeginnerAdd two numbers in C++ with cin and cout.Open example →cpp#include <iostream> using namespace std; int main() {