C++ Practice Programs: 50+ Solved University Lab Exercises & Real-World Code

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.

Category A: Core Fundamentals & Math

Program 1: Check Palindrome Number

#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;
}
Output: 12321 is a Palindrome Number!

Program 2: Prime Number Generator within Range [1, N]

#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;
}
Output: Primes up to 20: 2 3 5 7 11 13 17 19

Category B: Arrays, Matrices & Strings

Program 3: 2D Matrix Multiplication ($3 \times 3$)

#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;
}
Output:
19 22
43 50

Category C: Object-Oriented Programming (OOP) Labs

Program 4: University Student Records with Constructors

#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;
}

Program 5: Polymorphic Area Calculator (Pure Virtual Functions)

#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;
}

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What are the most frequent C++ programs asked in BCA university lab exams?

The most frequent BCA lab exam programs are: 1. Prime number check & Fibonacci series, 2. Palindrome string & number check, 3. Matrix multiplication, 4. Function & Operator overloading (Complex numbers), 5. Single & Multiple inheritance with constructors, 6. Pure virtual functions for area calculation of shapes.

Q2. How can I practice these C++ programs on my computer?

You can copy the code directly into any C++ compiler (VS Code with g++, Visual Studio, CLion, or online compilers like Compiler Explorer / GDB). Compile with 'g++ -std=c++20 program.cpp' to ensure full modern C++ support.

Explore Related C++ Tutorials & Next Steps

Boost your C++ proficiency by studying these closely connected core modules:

100+ Interview Q&A

Top technical questions asked in campus placements.

Code Challenges

Debugging drills and practical challenges.

Quick Reference

Syntax and complexity cheat sheet.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!