Theory without hands-on practice is incomplete. Here is a curated, categorized master library of 50+ classic and modern C++ programs with full source code, detailed logic walkthroughs, and expected terminal outputs. Ideal for BCA, B.Sc. IT, B.Tech CS semester exams, and competitive programming beginners.
#include <iostream>
int main() {
int n = 12321, original = n, reversed = 0;
while (n > 0) {
reversed = (reversed * 10) + (n % 10);
n /= 10;
}
if (original == reversed) {
std::cout << original << " is a Palindrome Number!\n";
} else {
std::cout << original << " is NOT a palindrome.\n";
}
return 0;
}
12321 is a Palindrome Number!#include <iostream>
#include <vector>
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) return false;
}
return true;
}
int main() {
int limit = 20;
std::cout << "Primes up to " << limit << ": ";
for (int i = 2; i <= limit; ++i) {
if (isPrime(i)) std::cout << i << " ";
}
std::cout << "\n";
return 0;
}
Primes up to 20: 2 3 5 7 11 13 17 19#include <iostream>
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2] = {0};
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
std::cout << "Matrix Multiplication Result:\n";
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
std::cout << C[i][j] << " ";
}
std::cout << "\n";
}
return 0;
}
19 2243 50
#include <iostream>
#include <string>
#include <vector>
class Student {
private:
int roll;
std::string name;
float marks[3];
public:
Student(int r, std::string n, float m1, float m2, float m3)
: roll(r), name(n) {
marks[0] = m1; marks[1] = m2; marks[2] = m3;
}
float getAverage() const {
return (marks[0] + marks[1] + marks[2]) / 3.0f;
}
void display() const {
std::cout << "Roll: " << roll << " | Name: " << name
<< " | Avg: " << getAverage() << "\n";
}
};
int main() {
std::vector<Student> roster = {
Student(101, "Aarav Sharma", 88.5, 92.0, 79.5),
Student(102, "Priya Patel", 95.0, 94.5, 98.0)
};
std::cout << "--- Student Grade Summary ---\n";
for (const auto& s : roster) {
s.display();
}
return 0;
}
#include <iostream>
#include <memory>
#include <vector>
class Shape {
public:
virtual double area() const = 0; // Pure virtual
virtual ~Shape() = default;
};
class Rectangle : public Shape {
double w, h;
public:
Rectangle(double width, double height) : w(width), h(height) {}
double area() const override { return w * h; }
};
class Circle : public Shape {
double r;
public:
Circle(double radius) : r(radius) {}
double area() const override { return 3.14159 * r * r; }
};
int main() {
std::vector<std::unique_ptr<Shape>> shapes;
shapes.push_back(std::make_unique<Rectangle>(10.0, 5.0));
shapes.push_back(std::make_unique<Circle>(7.0));
for (const auto& s : shapes) {
std::cout << "Calculated Area: " << s->area() << "\n";
}
return 0;
}
Boost your C++ proficiency by studying these closely connected core modules:
Top technical questions asked in campus placements.
Debugging drills and practical challenges.
Syntax and complexity cheat sheet.