Welcome to The Coding College! In this tutorial, we’ll cover a variety of practical C++ examples to help you understand key concepts in programming. These examples demonstrate the use of core C++ features such as variables, loops, functions, and object-oriented programming (OOP). Each example is beginner-friendly, providing a solid foundation for learning C++.
1. Hello, World!
The classic starting point for any programming language.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl; // Print a message
return 0;
}
Output:
Hello, World!
2. Add Two Numbers
#include <iostream>
using namespace std;
int main() {
int num1, num2, sum;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
sum = num1 + num2; // Add the numbers
cout << "Sum = " << sum << endl;
return 0;
}
Input:
10 20
Output:
Sum = 30
3. Check Even or Odd
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}
return 0;
}
Input:
7
Output:
7 is odd.
4. Factorial of a Number
#include <iostream>
using namespace std;
int main() {
int n, factorial = 1;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
cout << "Factorial of " << n << " is " << factorial << endl;
return 0;
}
Input:
5
Output:
Factorial of 5 is 120
5. Find the Largest Number
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
int largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
cout << "The largest number is " << largest << endl;
return 0;
}
Input:
15 45 10
Output:
The largest number is 45
6. Prime Number Check
#include <iostream>
using namespace std;
int main() {
int n, i;
bool isPrime = true;
cout << "Enter a positive integer: ";
cin >> n;
if (n <= 1) {
isPrime = false;
} else {
for (i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
cout << n << " is a prime number." << endl;
else
cout << n << " is not a prime number." << endl;
return 0;
}
Input:
29
Output:
29 is a prime number.
7. Reverse a String
#include <iostream>
#include <string>
using namespace std;
int main() {
string str, reversedStr;
cout << "Enter a string: ";
cin >> str;
reversedStr = string(str.rbegin(), str.rend());
cout << "Reversed string: " << reversedStr << endl;
return 0;
}
Input:
Hello
Output:
Reversed string: olleH
8. Simple Calculator
#include <iostream>
using namespace std;
int main() {
char operation;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> operation;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
switch (operation) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
if (num2 != 0)
cout << "Result: " << num1 / num2 << endl;
else
cout << "Division by zero is not allowed." << endl;
break;
default:
cout << "Invalid operator!" << endl;
}
return 0;
}
Input:
+
4 5
Output:
Result: 9
9. Fibonacci Series
#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; i++) {
cout << t1 << " ";
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Input:
6
Output:
Fibonacci Series: 0 1 1 2 3 5
10. File Handling
Write and read data from a file.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile("example.txt");
outFile << "Hello, file!" << endl;
outFile.close();
ifstream inFile("example.txt");
string line;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
return 0;
}
Output:
Hello, file!
Summary
These examples demonstrate a variety of tasks and concepts in C++ programming, from basic operations to advanced techniques. Use them as building blocks to develop your own programs and deepen your understanding of C++.
For more tutorials, visit The Coding College and continue mastering the art of coding! 🚀