Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Tuesday, August 18, 2026

August 18, 2026

Basic C Programing exercise before stating of CPP


AIM:

Write a C program to develop a menu-driven Student Result Management System. The program should accept marks of students in multiple subjects, calculate total and percentage, determine pass/fail status and grade using decision-making statements, and display the result. Use functions for different operations and appropriate loops for repeated processing. Implement a menu using switch-case and provide an option to terminate the program.


Code: 

 #include <stdio.h>

void result()

{

    int m[5], i, total = 0;

    float per;

    printf("Enter marks of 5 subjects: ");

    for(i = 0; i < 5; i++) {

        scanf("%d", &m[i]);

        total += m[i];

    }


    per = total / 5.0;


    printf("Total = %d\nPercentage = %.2f\n", total, per);


    if(per >= 90)

        printf("Grade: A+\n");

    else if(per >= 75)

        printf("Grade: A\n");

    else if(per >= 60)

        printf("Grade: B\n");

    else if(per >= 40)

        printf("Grade: C\n");

    else

        printf("Result: Fail\n");

}


int main()

{

    int ch;


    do {

        printf("\n1. Enter Result\n2. Exit\nChoice: ");

        scanf("%d", &ch);


        switch(ch) {

            case 1: result(); break;

            case 2: printf("Exit"); break;

            default: printf("Invalid Choice");

        }

    } while(ch != 2);


    return 0;

}


Output :






Monday, August 17, 2026

August 17, 2026

To develop C++ programs that randomly generate complex numbers using the previously designed Complex class, write two complex numbers per line in a file along with an arithmetic operator, and read the file line by line to perform the specified operation.

 

Experiment 10

Aim

To develop C++ programs that randomly generate complex numbers using the previously designed Complex class, write two complex numbers per line in a file along with an arithmetic operator, and read the file line by line to perform the specified operation.

Objectives

  • To use the previously designed Complex class.
  • To generate random complex numbers.
  • To perform file output and input operations.
  • To store arithmetic operators along with complex numbers.
  • To read and process data line by line.
  • To perform arithmetic operations on Complex objects.

Theory

File handling allows data to be stored permanently rather than only in main memory. C++ provides:

  • ofstream for writing to files.
  • ifstream for reading from files.

In this experiment, each line contains two complex numbers and an arithmetic operator.

For example:

(4 + 3i) (2 + 1i) +

The second program reads each line and calculates:

(4 + 3i) + (2 + 1i)

Program 1: Generate and Store Complex Numbers

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <ctime>

using namespace std;

 

class Complex {

    int r, i;

 

public:

    Complex(int x = 0, int y = 0) {

        r = x;

        i = y;

    }

 

    void write(ofstream &file) {

        file << "(" << r << " + " << i << "i)";

    }

};

 

int main() {

    ofstream file("complex.txt");

 

    srand(time(0));

 

    char op[] = {'+', '-', '*', '/'};

 

    for (int n = 0; n < 5; n++) {

        Complex a(rand() % 10, rand() % 10);

        Complex b(rand() % 10, rand() % 10);

 

        a.write(file);

        file << " " << op[rand() % 4] << " ";

        b.write(file);

        file << endl;

    }

 

    file.close();

 

    cout << "Complex numbers written to file.\n";

 

    return 0;

}

Program 2: Read File and Perform Operation

#include <iostream>

#include <fstream>

using namespace std;

 

class Complex {

    double r, i;

 

public:

    Complex(double x = 0, double y = 0) {

        r = x;

        i = y;

    }

 

    Complex operator+(Complex c) {

        return Complex(r + c.r, i + c.i);

    }

 

    Complex operator-(Complex c) {

        return Complex(r - c.r, i - c.i);

    }

 

    Complex operator*(Complex c) {

        return Complex(r*c.r - i*c.i,

                       r*c.i + i*c.r);

    }

 

    Complex operator/(Complex c) {

        double d = c.r*c.r + c.i*c.i;

 

        if (d == 0)

            throw "Division by zero";

 

        return Complex(

            (r*c.r + i*c.i) / d,

            (i*c.r - r*c.i) / d

        );

    }

 

    void display() {

        cout << "(" << r << " + " << i << "i)";

    }

};

 

int main() {

    ifstream file("complex.txt");

 

    double r1, i1, r2, i2;

    char ch, op;

    Complex result;

 

    while (file >> ch >> r1 >> ch >> i1 >> ch

                >> ch >> r2 >> ch >> i2 >> ch >> op) {

 

        Complex a(r1, i1), b(r2, i2);

 

        try {

            switch (op) {

                case '+':

                    result = a + b;

                    break;

 

                case '-':

                    result = a - b;

                    break;

 

                case '*':

                    result = a * b;

                    break;

 

                case '/':

                    result = a / b;

                    break;

            }

 

            a.display();

            cout << " " << op << " ";

            b.display();

            cout << " = ";

            result.display();

            cout << endl;

        }

        catch (const char *msg) {

            cout << "Exception: " << msg << endl;

        }

    }

 

    file.close();

 

    return 0;

}

Sample Output

Complex numbers written to file.

 

(4 + 3i) + (2 + 1i) = (6 + 4i)

(7 + 2i) - (3 + 5i) = (4 + -3i)

(2 + 4i) * (3 + 1i) = (2 + 14i)

(8 + 6i) / (2 + 2i) = (3.5 + -0.5i)

Result

Thus, complex numbers were successfully generated randomly, stored in a file along with arithmetic operators, read from the file, and processed using overloaded Complex class operators.

August 17, 2026

To design and implement Stack and Queue classes with necessary exception handling for handling overflow and underflow conditions.

 Experiment 9

Aim

To design and implement Stack and Queue classes with necessary exception handling for handling overflow and underflow conditions.

Objectives

  • To implement Stack and Queue using classes.
  • To understand exception handling.
  • To handle overflow and underflow conditions.
  • To use try, throw and catch.

Theory

An exception represents an abnormal condition during program execution. C++ provides three major keywords for exception handling:

  • try — contains code that may generate an exception.
  • throw — generates an exception.
  • catch — handles the exception.

Program

#include <iostream>

using namespace std;

 

class Stack {

    int a[5], top;

 

public:

    Stack() {

        top = -1;

    }

 

    void push(int x) {

        if (top == 4)

            throw "Stack Overflow";

 

        a[++top] = x;

    }

 

    void pop() {

        if (top == -1)

            throw "Stack Underflow";

 

        cout << "Popped: " << a[top--] << endl;

    }

 

    void display() {

        for (int i = top; i >= 0; i--)

            cout << a[i] << " ";

 

        cout << endl;

    }

};

 

class Queue {

    int a[5], front, rear;

 

public:

    Queue() {

        front = 0;

        rear = -1;

    }

 

    void insert(int x) {

        if (rear == 4)

            throw "Queue Overflow";

 

        a[++rear] = x;

    }

 

    void remove() {

        if (front > rear)

            throw "Queue Underflow";

 

        cout << "Deleted: " << a[front++] << endl;

    }

 

    void display() {

        for (int i = front; i <= rear; i++)

            cout << a[i] << " ";

 

        cout << endl;

    }

};

 

int main() {

    try {

        Stack s;

 

        s.push(10);

        s.push(20);

        s.push(30);

 

        cout << "Stack: ";

        s.display();

 

        s.pop();

 

        Queue q;

 

        q.insert(10);

        q.insert(20);

        q.insert(30);

 

        cout << "Queue: ";

        q.display();

 

        q.remove();

    }

    catch (const char *msg) {

        cout << "Exception: " << msg << endl;

    }

 

    return 0;

}

Experiment 9

Aim

To design and implement Stack and Queue classes with necessary exception handling for handling overflow and underflow conditions.

Objectives

  • To implement Stack and Queue using classes.
  • To understand exception handling.
  • To handle overflow and underflow conditions.
  • To use try, throw and catch.

Theory

An exception represents an abnormal condition during program execution. C++ provides three major keywords for exception handling:

  • try — contains code that may generate an exception.
  • throw — generates an exception.
  • catch — handles the exception.

Program

#include <iostream>

using namespace std;

 

class Stack {

    int a[5], top;

 

public:

    Stack() {

        top = -1;

    }

 

    void push(int x) {

        if (top == 4)

            throw "Stack Overflow";

 

        a[++top] = x;

    }

 

    void pop() {

        if (top == -1)

            throw "Stack Underflow";

 

        cout << "Popped: " << a[top--] << endl;

    }

 

    void display() {

        for (int i = top; i >= 0; i--)

            cout << a[i] << " ";

 

        cout << endl;

    }

};

 

class Queue {

    int a[5], front, rear;

 

public:

    Queue() {

        front = 0;

        rear = -1;

    }

 

    void insert(int x) {

        if (rear == 4)

            throw "Queue Overflow";

 

        a[++rear] = x;

    }

 

    void remove() {

        if (front > rear)

            throw "Queue Underflow";

 

        cout << "Deleted: " << a[front++] << endl;

    }

 

    void display() {

        for (int i = front; i <= rear; i++)

            cout << a[i] << " ";

 

        cout << endl;

    }

};

 

int main() {

    try {

        Stack s;

 

        s.push(10);

        s.push(20);

        s.push(30);

 

        cout << "Stack: ";

        s.display();

 

        s.pop();

 

        Queue q;

 

        q.insert(10);

        q.insert(20);

        q.insert(30);

 

        cout << "Queue: ";

        q.display();

 

        q.remove();

    }

    catch (const char *msg) {

        cout << "Exception: " << msg << endl;

    }

 

    return 0;

}

Result

Thus, Stack and Queue classes were successfully implemented with exception handling for overflow and underflow conditions.


August 17, 2026

To develop templates of standard sorting algorithms, namely Bubble Sort, Insertion Sort and Quick Sort.

 

Experiment 8

Aim

To develop templates of standard sorting algorithms, namely Bubble Sort, Insertion Sort and Quick Sort.

Objectives

  • To understand function templates.
  • To implement generic sorting algorithms.
  • To compare different sorting approaches.
  • To apply templates to array-based data.

Program

#include <iostream>

using namespace std;

 

template <class T>

void bubbleSort(T a[], int n) {

    for (int i = 0; i < n - 1; i++)

        for (int j = 0; j < n - i - 1; j++)

            if (a[j] > a[j + 1])

                swap(a[j], a[j + 1]);

}

 

template <class T>

void insertionSort(T a[], int n) {

    for (int i = 1; i < n; i++) {

        T key = a[i];

        int j = i - 1;

 

        while (j >= 0 && a[j] > key) {

            a[j + 1] = a[j];

            j--;

        }

 

        a[j + 1] = key;

    }

}

 

template <class T>

void quickSort(T a[], int low, int high) {

    if (low >= high)

        return;

 

    T pivot = a[high];

    int i = low - 1;

 

    for (int j = low; j < high; j++) {

        if (a[j] < pivot)

            swap(a[++i], a[j]);

    }

 

    swap(a[i + 1], a[high]);

 

    int p = i + 1;

 

    quickSort(a, low, p - 1);

    quickSort(a, p + 1, high);

}

 

template <class T>

void display(T a[], int n) {

    for (int i = 0; i < n; i++)

        cout << a[i] << " ";

 

    cout << endl;

}

 

int main() {

    int a[] = {5, 2, 8, 1, 3};

    int n = 5;

 

    cout << "Original: ";

    display(a, n);

 

    bubbleSort(a, n);

    cout << "Bubble Sort: ";

    display(a, n);

 

    int b[] = {5, 2, 8, 1, 3};

    insertionSort(b, n);

    cout << "Insertion Sort: ";

    display(b, n);

 

    int c[] = {5, 2, 8, 1, 3};

    quickSort(c, 0, n - 1);

    cout << "Quick Sort: ";

    display(c, n);

 

    return 0;

}

Result

Thus, Bubble Sort, Insertion Sort and Quick Sort were successfully implemented using function templates.

August 17, 2026

To develop a template of the Linked List class and its methods using C++ class templates.

 

Experiment 7

Aim

To develop a template of the Linked List class and its methods using C++ class templates.

Objectives

  • To understand class templates.
  • To implement a generic linked list.
  • To perform insertion, deletion and display operations.
  • To understand generic data structures.

Theory

Templates allow a class or function to operate on different data types without rewriting the code.

A linked list consists of nodes where each node contains data and a pointer to the next node.

Program

#include <iostream>

using namespace std;

 

template <class T>

class LinkedList {

    struct Node {

        T data;

        Node *next;

 

        Node(T x) {

            data = x;

            next = NULL;

        }

    };

 

    Node *head;

 

public:

    LinkedList() {

        head = NULL;

    }

 

    void insert(T x) {

        Node *n = new Node(x);

        n->next = head;

        head = n;

    }

 

    void remove() {

        if (head == NULL) {

            cout << "List is empty\n";

            return;

        }

 

        Node *temp = head;

        head = head->next;

        delete temp;

    }

 

    void display() {

        Node *p = head;

 

        while (p != NULL) {

            cout << p->data << " ";

            p = p->next;

        }

 

        cout << endl;

    }

};

 

int main() {

    LinkedList<int> list;

 

    list.insert(10);

    list.insert(20);

    list.insert(30);

 

    cout << "List: ";

    list.display();

 

    list.remove();

 

    cout << "After deletion: ";

    list.display();

 

    return 0;

}

Result

Thus, a generic Linked List class template and its basic methods were successfully implemented.

August 17, 2026

To design and implement a simple C++ application to demonstrate dynamic polymorphism and Run-Time Type Identification (RTTI).

 

Experiment 6

Aim

To design and implement a simple C++ application to demonstrate dynamic polymorphism and Run-Time Type Identification (RTTI).

Objectives

  • To understand runtime polymorphism.
  • To implement virtual functions.
  • To understand dynamic_cast.
  • To understand typeid and RTTI.

Theory

Dynamic polymorphism occurs when a base-class pointer or reference invokes an overridden function of a derived class at runtime.

RTTI allows the program to determine the actual type of an object during runtime. C++ provides typeid and dynamic_cast for this purpose.

Program

#include <iostream>

#include <typeinfo>

using namespace std;

 

class Shape {

public:

    virtual void display() {

        cout << "Shape\n";

    }

 

    virtual ~Shape() {}

};

 

class Circle : public Shape {

public:

    void display() override {

        cout << "Circle\n";

    }

 

    void circleInfo() {

        cout << "Circle specific function\n";

    }

};

 

class Rectangle : public Shape {

public:

    void display() override {

        cout << "Rectangle\n";

    }

};

 

int main() {

    Shape *ptr;

 

    Circle c;

    Rectangle r;

 

    ptr = &c;

 

    cout << "Dynamic Polymorphism:\n";

    ptr->display();

 

    if (dynamic_cast<Circle*>(ptr))

        cout << "Object is Circle\n";

 

    cout << "Runtime type: "

         << typeid(*ptr).name() << endl;

 

    ptr = &r;

 

    ptr->display();

 

    cout << "Runtime type: "

         << typeid(*ptr).name() << endl;

 

    return 0;

}

Result

Thus, dynamic polymorphism and RTTI using dynamic_cast and typeid were successfully demonstrated.

August 17, 2026

To develop a C++ class hierarchy for different types of inheritance, including single, multilevel, hierarchical and multiple inheritance.

 

Experiment 5

Aim

To develop a C++ class hierarchy for different types of inheritance, including single, multilevel, hierarchical and multiple inheritance.

Objectives

  • To understand inheritance.
  • To implement different forms of inheritance.
  • To understand code reusability through inheritance.
  • To develop a class hierarchy.

Theory

Inheritance allows one class to acquire the properties and behaviour of another class.

Major types include:

  • Single inheritance
  • Multilevel inheritance
  • Multiple inheritance
  • Hierarchical inheritance

Program

#include <iostream>

using namespace std;

 

class Person {

public:

    void person() {

        cout << "Person\n";

    }

};

 

class Student : public Person {

public:

    void student() {

        cout << "Student\n";

    }

};

 

class Result : public Student {

public:

    void result() {

        cout << "Result\n";

    }

};

 

class Teacher : public Person {

public:

    void teacher() {

        cout << "Teacher\n";

    }

};

 

class Sports {

public:

    void sports() {

        cout << "Sports\n";

    }

};

 

class CollegeStudent : public Student, public Sports {

public:

    void collegeStudent() {

        cout << "College Student\n";

    }

};

 

int main() {

    cout << "Multilevel Inheritance:\n";

    Result r;

    r.person();

    r.student();

    r.result();

 

    cout << "\nHierarchical Inheritance:\n";

    Teacher t;

    t.person();

    t.teacher();

 

    cout << "\nMultiple Inheritance:\n";

    CollegeStudent c;

    c.student();

    c.sports();

    c.collegeStudent();

 

    return 0;

}

Result

Thus, a C++ class hierarchy was successfully developed to demonstrate different forms of inheritance.