EXAMPLES11 MIN READUPDATED Jun 4, 2026

C++ LAB PROGRAMS WITH OUTPUT

15 C++ programs that show up in lab manuals again and again. Each one has the code, a sample output, and a one-line explanation.

T

The Assignment Bot Team

Jun 4, 2026 · Editorial

These 15 C++ programs are the ones you will see most often in undergraduate lab manuals. Each one is a complete, working program you can paste into your IDE, compile, and run. We have grouped them by topic so you can find the one you need.

i

How to use this page

Each program is a self-contained snippet. Copy it into a file named programN.cpp, compile with g++, and run. The output shown is what you should see when you run the program with the input shown in the comment.

1. Hello, World

cppCODE
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

Output: Hello, World!

2. Add Two Numbers

cppCODE
#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    cout << "Sum = " << a + b << endl;
    return 0;
}

Input: 5 7 — Output: Sum = 12

3. Find the Largest of Three Numbers

cppCODE
#include <iostream>
using namespace std;

int main() {
    int a, b, c;
    cout << "Enter three numbers: ";
    cin >> a >> b >> c;

    int largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
    cout << "Largest = " << largest << endl;
    return 0;
}

Input: 12 45 23 — Output: Largest = 45

4. Check if a Number is Prime

cppCODE
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;

    bool isPrime = true;
    if (n <= 1) isPrime = false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            isPrime = false;
            break;
        }
    }
    cout << (isPrime ? "Prime" : "Not Prime") << endl;
    return 0;
}

Input: 29 — Output: Prime. Input: 30 — Output: Not Prime.

5. Print the Fibonacci Series

cppCODE
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of terms: ";
    cin >> n;

    int a = 0, b = 1;
    cout << "Fibonacci Series: " << a << " " << b << " ";
    for (int i = 2; i < n; i++) {
        int c = a + b;
        cout << c << " ";
        a = b;
        b = c;
    }
    cout << endl;
    return 0;
}

Input: 8 — Output: Fibonacci Series: 0 1 1 2 3 5 8 13

6. Bubble Sort

cppCODE
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of elements: ";
    cin >> n;
    int arr[100];
    for (int i = 0; i < n; i++) cin >> arr[i];

    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                swap(arr[j], arr[j + 1]);
            }
        }
    }

    cout << "Sorted: ";
    for (int i = 0; i < n; i++) cout << arr[i] << " ";
    cout << endl;
    return 0;
}

Input: 5 then 64 34 25 12 22 — Output: Sorted: 12 22 25 34 64

cppCODE
#include <iostream>
using namespace std;

int main() {
    int n, key;
    cout << "Enter size and key: ";
    cin >> n >> key;
    int arr[100];
    for (int i = 0; i < n; i++) cin >> arr[i];

    int found = -1;
    for (int i = 0; i < n; i++) {
        if (arr[i] == key) {
            found = i;
            break;
        }
    }
    cout << (found == -1 ? "Not found" : "Found at index " + to_string(found)) << endl;
    return 0;
}

8. Reverse a String

cppCODE
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    string s;
    cout << "Enter a string: ";
    cin >> s;
    reverse(s.begin(), s.end());
    cout << "Reversed: " << s << endl;
    return 0;
}

9. Stack Using Array

cppCODE
#include <iostream>
using namespace std;

#define MAX 100

int stack[MAX];
int top = -1;

void push(int val) {
    if (top == MAX - 1) { cout << "Overflow" << endl; return; }
    stack[++top] = val;
}

int pop() {
    if (top == -1) { cout << "Underflow" << endl; return -1; }
    return stack[top--];
}

void display() {
    for (int i = top; i >= 0; i--) cout << stack[i] << " ";
    cout << endl;
}

int main() {
    push(10); push(20); push(30);
    cout << "Stack: "; display();
    cout << "Popped: " << pop() << endl;
    cout << "Stack: "; display();
    return 0;
}

Output: Stack: 30 20 10 — Popped: 30 — Stack: 20 10

10. Queue Using Array

cppCODE
#include <iostream>
using namespace std;

#define MAX 100

int queue[MAX];
int front = 0, rear = -1;

void enqueue(int val) {
    if (rear == MAX - 1) { cout << "Overflow" << endl; return; }
    queue[++rear] = val;
}

int dequeue() {
    if (front > rear) { cout << "Underflow" << endl; return -1; }
    return queue[front++];
}

int main() {
    enqueue(10); enqueue(20); enqueue(30);
    cout << "Dequeued: " << dequeue() << endl;
    cout << "Dequeued: " << dequeue() << endl;
    return 0;
}

Output: Dequeued: 10 — Dequeued: 20

11. Factorial Using Recursion

cppCODE
#include <iostream>
using namespace std;

int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << "Factorial = " << factorial(n) << endl;
    return 0;
}

Input: 5 — Output: Factorial = 120

12. Palindrome Check

cppCODE
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cout << "Enter a string: ";
    cin >> s;

    bool isPalin = true;
    int n = s.length();
    for (int i = 0; i < n / 2; i++) {
        if (s[i] != s[n - 1 - i]) {
            isPalin = false;
            break;
        }
    }
    cout << (isPalin ? "Palindrome" : "Not Palindrome") << endl;
    return 0;
}

13. Matrix Addition

cppCODE
#include <iostream>
using namespace std;

int main() {
    int r, c;
    cout << "Enter rows and columns: ";
    cin >> r >> c;
    int a[10][10], b[10][10], sum[10][10];

    cout << "Enter matrix A:\n";
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++) cin >> a[i][j];

    cout << "Enter matrix B:\n";
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++) cin >> b[i][j];

    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++) sum[i][j] = a[i][j] + b[i][j];

    cout << "Sum:\n";
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) cout << sum[i][j] << " ";
        cout << endl;
    }
    return 0;
}
cppCODE
#include <iostream>
using namespace std;

int main() {
    int n, key;
    cout << "Enter size and key: ";
    cin >> n >> key;
    int arr[100];
    for (int i = 0; i < n; i++) cin >> arr[i];

    int lo = 0, hi = n - 1, found = -1;
    while (lo <= hi) {
        int mid = (lo + hi) / 2;
        if (arr[mid] == key) { found = mid; break; }
        else if (arr[mid] < key) lo = mid + 1;
        else hi = mid - 1;
    }
    cout << (found == -1 ? "Not found" : "Found at index " + to_string(found)) << endl;
    return 0;
}

15. Linked List — Insert and Display

cppCODE
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};

int main() {
    Node* head = nullptr;
    Node* tail = nullptr;

    for (int i = 0; i < 3; i++) {
        int val;
        cin >> val;
        Node* n = new Node{val, nullptr};
        if (!head) head = tail = n;
        else { tail->next = n; tail = n; }
    }

    cout << "List: ";
    for (Node* cur = head; cur; cur = cur->next) cout << cur->data << " ";
    cout << endl;
    return 0;
}

Input: 10 20 30 — Output: List: 10 20 30

What to Do With These Programs

Once you have a working program, the only remaining work is the lab report itself: Aim, Theory, Algorithm, Code (which you already have), Output (a screenshot of the program running), and Conclusion. Assignment Bot generates that write-up from a screenshot of your brief in 10 minutes.

Skip the writing

Upload your C++ brief. Get a complete, formatted DOCX in 10 minutes. First assignment is free.

ABOUT THE AUTHOR

The Assignment Bot TeamWe test, write, and ship practical guides for CS students who want to spend less time formatting and more time learning.

READY TO SHIP YOUR LAB REPORT?

Skip the formatting. Upload your brief, get a complete, submit-ready DOCX in 10 minutes. First assignment is free.