🧪 C++ Examples – Learn by Doing!
The best way to learn C++ is by writing and running code. Below are a bunch of simple, real-life examples to help you understand how things work in the C++ world. 🎯
🔢 Example: Add Two Numbers
#include <iostream> using namespace std; int main() { int a = 5, b = 10; int sum = a + b; cout << "Sum: " << sum << endl; return 0; }
📐 Example: Area of a Rectangle
#include <iostream> using namespace std; int main() { int length = 8, width = 4; int area = length * width; cout << "Area: " << area << endl; return 0; }
📊 Example: Check Even or Odd
#include <iostream> using namespace std; int main() { int number = 7; if (number % 2 == 0) cout << number << " is even." << endl; else cout << number << " is odd." << endl; return 0; }
⏱️ Example: Simple Loop
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { cout << "Count: " << i << endl; } return 0; }
🌟 Example: Star Triangle
#include <iostream> using namespace std; int main() { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { cout << "* "; } cout << endl; } return 0; }
⬛ Example: Print Square Pattern
#include <iostream> using namespace std; int main() { int size = 4; for (int i = 1; i <= size; i++) { for (int j = 1; j <= size; j++) { cout << "* "; } cout << endl; } return 0; }
🔢 Example: Fibonacci Series
#include <iostream> using namespace std; int main() { int n = 10; int t1 = 0, t2 = 1, nextTerm; cout << "Fibonacci Series: " << t1 << ", " << t2; for (int i = 3; i <= n; ++i) { nextTerm = t1 + t2; cout << ", " << nextTerm; t1 = t2; t2 = nextTerm; } cout << endl; return 0; }
🎯 Example: Number Guessing Game
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); // Random seed int secret = rand() % 10 + 1; int guess; cout << "Guess a number between 1 and 10: "; cin >> guess; if (guess == secret) { cout << "🎉 Wow! You guessed it right!"; } else { cout << "Oops! The number was " << secret << ". Try again!"; } return 0; }
🔄 Example: Swap Two Numbers Without Temp
#include <iostream> using namespace std; int main() { int a = 5, b = 10; cout << "Before Swap: a = " << a << ", b = " << b << endl; a = a + b; b = a - b; a = a - b; cout << "After Swap: a = " << a << ", b = " << b << endl; return 0; }
Code Output
Before Swap: a = 5, b = 10 After Swap: a = 10, b = 5
Explanation
-
a = a + b;
→a = 15
-
b = a - b;
→b = 15 - 10 = 5
(b gets the original value of a) -
a = a - b;
→a = 15 - 5 = 10
(a gets the original value of b)
🔁 Example: Reverse a Number
#include <iostream> using namespace std; int main() { int num = 12345, rev = 0; while (num != 0) { rev = rev * 10 + num % 10; num /= 10; } cout << "Reversed Number = " << rev << endl; return 0; }
🧮 Example: Simple Calculator
#include <iostream> using namespace std; int main() { char op; float a, b; cout << "Enter operator (+, -, *, /): "; cin >> op; cout << "Enter two numbers: "; cin >> a >> b; switch(op) { case '+': cout << "Result: " << a + b; break; case '-': cout << "Result: " << a - b; break; case '*': cout << "Result: " << a * b; break; case '/': if (b != 0) cout << "Result: " << a / b; else cout << "Division by zero not allowed!"; break; default: cout << "Invalid operator!"; } return 0; }
🧠 Tip:
Try changing the values in each program and run them again! Playing with the code is the best way to remember it. 💡