loader image

C++ Laboratory BCS306B

#include <iostream>
using namespace std;
int  main() {
    double n1, n2, n3;
    cout << "Enter any three numbers: ";
   cin >> n1 >> n2 >> n3;
   if(n1 >= n2 && n1 >= n3)
      cout << "Largest number is: " << n1;
    else if(n2 >= n1 && n2 >= n3)
        cout << "Largest number is: " << n2;
    else 
      cout << "Largest number is: " << n3;
return 0;
}

OUTPUT:

Enter any three numbers: 
3
2
1

Largest number is: 3
#include <iostream>
using namespace std;

int main() {
    int num[100], n;
    int i, j, man;
    
    cout << "Enter the numbers you want to sort:" << endl << endl;
    cin >> n;
    
    for (i = 0; i < n; i++) {       
        cout << "Enter number: ";
        cin >> num[i];
    }
    
    // Ascending Bubble Sort
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            if (num[i] < num[j]) {
                man = num[i];
                num[i] = num[j];
                num[j] = man;
            }          
        }
    }
    
    cout << "Ascending: " << endl;
    for (i = 0; i < n; i++) {
        cout << num[i] << " ";
    }
    
    // Descending Bubble Sort
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            if (num[i] > num[j]) {
                man = num[i];
                num[i] = num[j];
                num[j] = man;
            }          
        }
    }
    
    cout << endl << "Descending: " << endl;
    for (i = 0; i < n; i++) {
        cout << num[i] << " ";
    }
    
    return 0;
}

OUTPUT:

Enter the numbers you want to sort: 6

Enter number: 4
Enter number: 3
Enter number: 6
Enter number: 8
Enter number: 1
Enter number: 2

Ascending: 
1 2 3 4 6 8 

Descending: 
8 6 4 3 2 1
#include <iostream>
#include <string>

class Student {
public:
    void setStudentInfo(const std::string& name, const std::string& rollNumber, double marksSubject1, double marksSubject2) {
        studentName = name;
        studentRollNumber = rollNumber;
        marks1 = marksSubject1;
        marks2 = marksSubject2;
        calculateTotal(); // Calculate total marks
    }

    void displayStudentInfo() {
        std::cout << "Student Name: " << studentName << std::endl;
        std::cout << "Roll Number: " << studentRollNumber << std::endl;
        std::cout << "Marks in Subject 1: " << marks1 << std::endl;
        std::cout << "Marks in Subject 2: " << marks2 << std::endl;
        std::cout << "Total Score: " << totalScore << std::endl;
    }

private:
    std::string studentName;
    std::string studentRollNumber;
    double marks1;
    double marks2;
    double totalScore;

    void calculateTotal() {
        totalScore = marks1 + marks2;
    }
};

int main() {
    std::string name, rollNumber;
    double marks1, marks2;

    std::cout << "Enter student name: ";
    std::getline(std::cin, name);

    std::cout << "Enter student roll number: ";
    std::getline(std::cin, rollNumber);

    std::cout << "Enter marks in Subject 1: ";
    std::cin >> marks1;

    std::cout << "Enter marks in Subject 2: ";
    std::cin >> marks2;

    Student student;
    student.setStudentInfo(name, rollNumber, marks1, marks2);
    student.displayStudentInfo();

    return 0;
}

OUTPUT:

Enter student name: Braham Kumar
Enter student roll number: 1ME21CS017
Enter marks in Subject 1: 89
Enter marks in Subject 2: 87

Student Name: Braham Kumar
Roll Number: 1ME21CS017
Marks in Subject 1: 89
Marks in Subject 2: 87
Total Score: 176
#include <iostream>
#include <string>

class BankEmployee {
public:
    void setEmployeeInfo(const std::string& name, int accountNo, double balance) {
        employeeName = name;
        accountNumber = accountNo;
        currentBalance = balance;
    }
    void displayEmployeeInfo() {
        std::cout << "Employee Name: " << employeeName << std::endl;
        std::cout << "Account Number: " << accountNumber << std::endl;
        std::cout << "Balance: " << currentBalance << std::endl;

        if (currentBalance < 500.0) {
            std::cout << "Invalid Balance" << std::endl;
        }
    }
    void withdraw(double amount) {
        if (amount > 0 && amount <= currentBalance) {
            currentBalance -= amount;
            std::cout << "Withdrawn: " << amount << std::endl;
            std::cout << "Updated Balance: " << currentBalance << std::endl;
        } else {
            std::cout << "Invalid Withdrawal Amount" << std::endl;
        }
    }
    void deposit(double amount) {
        if (amount > 0) {
            currentBalance += amount;
            std::cout << "Deposited: " << amount << std::endl;
            std::cout << "Updated Balance: " << currentBalance << std::endl;
        } else {
            std::cout << "Invalid Deposit Amount" << std::endl;
        }
    }

private:
    std::string employeeName;
    int accountNumber;
    double currentBalance;
};

int main() {
    std::string name;
    int accountNo;
    double balance;

    std::cout << "Enter employee name: ";
    std::getline(std::cin, name);

    std::cout << "Enter account number: ";
    std::cin >> accountNo;

    std::cout << "Enter current balance: ";
    std::cin >> balance;

    BankEmployee employee;
    employee.setEmployeeInfo(name, accountNo, balance);

    employee.displayEmployeeInfo();

    double withdrawalAmount, depositAmount;
    std::cout << "Enter withdrawal amount: ";
    std::cin >> withdrawalAmount;
    employee.withdraw(withdrawalAmount);

    std::cout << "Enter deposit amount: ";
    std::cin >> depositAmount;
    employee.deposit(depositAmount);

    return 0;
}

OUTPUT:

**********************OUTPUT 1*********************************

Enter employee name: Braham Kumar
Enter account number: 001234567
Enter current balance: 2000

Employee Name: Braham Kumar
Account Number: 1234567
Balance: 2000
Enter withdrawal amount: 500
Withdrawn: 500
Updated Balance: 1500

Enter deposit amount: 1000
Deposited: 1000
Updated Balance: 2500


**********************OUTPUT 2*********************************

Enter employee name: Braham Kumar
Enter account number: 001234567
Enter current balance: 200

Employee Name: Braham Kumar
Account Number: 1234567
Balance: 200
Invalid Balance
Enter withdrawal amount: 500
Invalid Withdrawal Amount

Enter deposit amount: 100
Deposited: 100
Updated Balance: 300
#include <iostream>

int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

int main() {
    int intResult;
    double doubleResult;
    int int1, int2;
    double double1, double2;

    std::cout << "Enter two integers: ";
    std::cin >> int1 >> int2;

    std::cout << "Enter two doubles: ";
    std::cin >> double1 >> double2;

    intResult = add(int1, int2);          
    doubleResult = add(double1, double2);

    std::cout << "Integer Addition Result: " << intResult << std::endl;
    std::cout << "Double Addition Result: " << doubleResult << std::endl;

    return 0;
}

OUTPUT:

Enter two integers:
9
5
Enter two doubles:
10
15

Integer Addition Result: 14
Double Addition Result: 25
#include <iostream>

class MyNumber {
private:
    int value;

public:
    MyNumber(int val) : value(val) {}

    MyNumber operator-() {
        return MyNumber(-value);
    }

    void display() {
        std::cout << "Value: " << value << std::endl;
    }
};

int main() {
    int initialValue;
    std::cout << "Enter the initial value for Number: ";
    std::cin >> initialValue;

    MyNumber num1(initialValue);
    MyNumber num2 = -num1; // Using unary minus operator
    std::cout << "Original Number:" << std::endl;
    num1.display();
    std::cout << "Number after Unary Minus Overloading:" << std::endl;
    num2.display();

    return 0;
}

OUTPUT:

Enter the initial value for Number: 5
Original Number:
Value: 5
Number after Unary Minus Overloading:
Value: -5
#include <iostream>

class Addition {
public:
    int add(int a, int b) {
        return a + b;
    }
};

class Subtraction {
public:
    int subtract(int a, int b) {
        return a - b;
    }
};

class Arithmetic : public Addition, public Subtraction {
public:
    int multiply(int a, int b) {
        return a * b;
    }

    int divide(int a, int b) {
        if (b != 0) {
            return a / b;
        } else {
            std::cout << "Error: Division by zero" << std::endl;
            return 0;
        }
    }
};

int main() {
    Arithmetic calculator;
    int num1, num2;

    std::cout << "Enter the first number: ";
    std::cin >> num1;

    std::cout << "Enter the second number: ";
    std::cin >> num2;

    int sum = calculator.add(num1, num2);
    std::cout << "Sum: " << sum << std::endl;

    int difference = calculator.subtract(num1, num2);
    std::cout << "Difference: " << difference << std::endl;

    int product = calculator.multiply(num1, num2);
    std::cout << "Product: " << product << std::endl;

    int quotient = calculator.divide(num1, num2);
    std::cout << "Quotient: " << quotient << std::endl;

    return 0;
}

OUTPUT:

Enter the first number: 6
Enter the second number: 7
Sum: 13
Difference: -1
Product: 42
Quotient: 0
#include <iostream>

class Alpha {
protected:
    int alpha;
public:
    Alpha(int a) : alpha(a) {
    }

    void displayAlpha() {
        std::cout << "Alpha: " << alpha << std::endl;
    }
};

class Beta : public Alpha {
private:
    int beta;

public:
    Beta(int a, int b) : Alpha(a), beta(b) {
    }

    void displayBeta() {
        displayAlpha(); 
        std::cout << "Beta: " << beta << std::endl;
    }
};

class Gamma : public Alpha {
private:
    int gamma;

public:
    Gamma(int a, int g) : Alpha(a), gamma(g) {
    }

    void displayGamma() {
        displayAlpha(); 
        std::cout << "Gamma: " << gamma << std::endl;
    }
};

int main() {
    Beta objBeta(10, 20);
    objBeta.displayBeta();

    Gamma objGamma(30, 40);
    objGamma.displayGamma();

    return 0;
}

OUTPUT:

Alpha: 10
Beta: 20
Alpha: 30
Gamma: 40
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ofstream outputFile("sample.txt");
    if (!outputFile.is_open()) {
        std::cerr << "Error: File could not be created." << std::endl;
        return 1;
    }

    std::string textToWrite = "Welcome to vtucode, Learn Easy for Engineering Students.";
    outputFile << textToWrite << std::endl;
    outputFile.close();

    std::ifstream inputFile("sample.txt");
    if (!inputFile.is_open()) {
        std::cerr << "Error: File could not be opened for reading." << std::endl;
        return 1;
    }

    std::string textRead;
    while (std::getline(inputFile, textRead)) {
        std::cout << "Text from the file: " << textRead << std::endl;
    }
    inputFile.close();

    return 0;
}

OUTPUT:

**********************OUTPUT 1*********************************

Text from the file: Welcome to vtucode, Learn Easy for Engineering Students.


**********************OUTPUT 2*********************************

Error: File could not be opened for reading
#include <iostream>
#include <fstream>
#include <ctime>

// Function to write time to a binary file
void writeTimeToFile(const char* fileName) {
    std::ofstream outFile(fileName, std::ios::binary);
    if (!outFile) {
        std::cerr << "Error: Unable to open file for writing." << std::endl;
        return;
    }

    // Get the current time
    time_t currentTime = time(nullptr);

    // Write the time to the file
    outFile.write(reinterpret_cast<const char*>(&currentTime), sizeof(currentTime));

    // Close the file
    outFile.close();

    std::cout << "Time has been written to the file." << std::endl;
}

// Function to read time from a binary file
void readTimeFromFile(const char* fileName) {
    std::ifstream inFile(fileName, std::ios::binary);
    if (!inFile) {
        std::cerr << "Error: Unable to open file for reading." << std::endl;
        return;
    }

    // Read the time from the file
    time_t storedTime;
    inFile.read(reinterpret_cast<char*>(&storedTime), sizeof(storedTime));

    // Check if reading was successful
    if (inFile.gcount() == sizeof(storedTime)) {
        // Convert time to string
        std::string timeString = std::ctime(&storedTime);

        // Output the read time
        std::cout << "Time read from file: " << timeString;
    } else {
        std::cerr << "Error: Unable to read time from file." << std::endl;
    }

    // Close the file
    inFile.close();
}

int main() {
    const char* fileName = "time_data.bin";

    // Write time to file
    writeTimeToFile(fileName);

    // Read time from file
    readTimeFromFile(fileName);

    return 0;
}

OUTPUT:

Time has been written to the file.
Time read from file: Sun Feb 11 14:34:07 2024
#include <iostream>
#include <stdexcept>
int divide(int numerator, int denominator) {
    if (denominator == 0) {
        throw std::runtime_error("Division by zero is not allowed.");
    }
    return numerator / denominator;
}

int main() {
    int numerator, denominator;

    std::cout << "Enter numerator: ";
    std::cin >> numerator;

    std::cout << "Enter denominator: ";
    std::cin >> denominator;

    try {
        int result = divide(numerator, denominator);
        std::cout << "Result of division: " << result << std::endl;
    } catch (const std::runtime_error &ex) {
        std::cerr << "Exception caught: " << ex.what() << std::endl;
    }

    return 0;
}

OUTPUT:

**********************OUTPUT 1*********************************

Enter numerator: 10
Enter denominator: 2
Result of division: 5


**********************OUTPUT 2*********************************

Enter numerator: 12
Enter denominator: 0
Exception caught: Division by zero is not allowed.
#include <iostream>
#include <vector>
#include <stdexcept>
int main() {
    try {
        std::vector<int> numbers = {1, 2, 3, 4, 5};
        int index;
        std::cout << "Enter an index to access an element in the array: ";
        std::cin >> index;
        if (index < 0 || index >= numbers.size()) {
            throw std::out_of_range("Index is out of bounds.");
        }
        int element = numbers.at(index);
        std::cout << "Element at index " << index << " is: " << element << std::endl;
    } catch (const std::out_of_range &ex) {
        std::cerr << "Exception caught: " << ex.what() << std::endl;
    }
    return 0;
}

OUTPUT:

**********************OUTPUT 1*********************************

Enter an index to access an element in the array: 3
Element at index 3 is: 4



**********************OUTPUT 1*********************************

Enter an index to access an element in the array: 6
Exception caught: Index is out of bounds.

Leave a Reply

Your email address will not be published. Required fields are marked *