Final Lab File
Final Lab File
INDEX
Code:
#include <stdio.h>
#include <string.h>
#define MAX_EMPLOYEES 10
#define NAME_LENGTH 50
#define DESIGNATION_LENGTH 50
typedef struct {
int EmpNo;
char name[NAME_LENGTH];
float paygrade;
char Designation[DESIGNATION_LENGTH];
} Employee;
Employee employees[MAX_EMPLOYEES];
int employeeCount = 0;
void inputEmployeeDetails() {
if (employeeCount < MAX_EMPLOYEES) {
printf("Enter Employee Number: ");
scanf("%d", &employees[employeeCount].EmpNo);
printf("Enter Name: ");
scanf("%s", employees[employeeCount].name);
printf("Enter Paygrade: ");
scanf("%f", &employees[employeeCount].paygrade);
printf("Enter Designation: ");
scanf("%s", employees[employeeCount].Designation);
employeeCount++;
} else {
printf("Employee array is full.\n");
}
}
void displayEmployeeDetails() {
for (int i = 0; i < employeeCount; i++) {
printf("Employee Number: %d\n", employees[i].EmpNo);
printf("Name: %s\n", employees[i].name);
printf("Paygrade: %.2f\n", employees[i].paygrade);
printf("Designation: %s\n", employees[i].Designation);
printf("\n");
}
}
void calculateTax() {
for (int i = 0; i < employeeCount; i++) {
float tax;
if (employees[i].paygrade <= 25000) {
tax = 0;
} else if (employees[i].paygrade <= 50000) {
tax = 0.05 * employees[i].paygrade;
} else if (employees[i].paygrade <= 100000) {
tax = 0.10 * employees[i].paygrade;
} else if (employees[i].paygrade <= 150000) {
tax = 0.15 * employees[i].paygrade;
} else {
tax = 0.30 * employees[i].paygrade;
}
printf("Employee Number: %d, Tax: %.2f\n", employees[i].EmpNo, tax);
MONISH RAI, 23/CS/266, CSE-4, G-2
}
}
void searchEmployee() {
int empNo;
printf("Enter Employee Number to search: ");
scanf("%d", &empNo);
for (int i = 0; i < employeeCount; i++) {
if (employees[i].EmpNo == empNo) {
printf("Employee Number: %d\n", employees[i].EmpNo);
printf("Name: %s\n", employees[i].name);
printf("Paygrade: %.2f\n", employees[i].paygrade);
printf("Designation: %s\n", employees[i].Designation);
return;
}
}
printf("Employee with number %d not found.\n", empNo);
}
int main() {
int choice;
while (1) {
printf("Menu:\n");
printf("1. Input Employee Details\n");
printf("2. Display All Employee Details\n");
printf("3. Calculate Tax\n");
printf("4. Search Employee\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
inputEmployeeDetails();
break;
case 2:
displayEmployeeDetails();
break;
case 3:
calculateTax();
break;
case 4:
searchEmployee();
break;
case 5:
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
MONISH RAI, 23/CS/266, CSE-4, G-2
Output:
MONISH RAI, 23/CS/266, CSE-4, G-2
Code:
# include <iostream>
using namespace std;
class basic {
int val;
public:
void update_val(int data) {
val = data;
}
void get_val(){
cout<<val<<endl;
}
};
int main()
{
basic num;
num.update_val(2);
num.get_val();
return 0;
}
Output:
MONISH RAI, 23/CS/266, CSE-4, G-2
Code:
#include <iostream>
using namespace std;
class Number
{
int a;
public:
// Default Constructor
Number()
{
this->a = 0;
}
// Parameterized Constructor
Number(int a)
{
this->a = a;
}
// Copy Constructor
Number(Number &obj)
{
cout << "Copy constructor is called" << endl;
this->a = obj.a;
}
void display()
{
cout << "Number is --> " << a << endl;
}
};
int main()
{
Number x, y, z(45);
x.display();
y.display();
z.display();
Number z1(z);
z1.display();
return 0;
}
Output:
MONISH RAI, 23/CS/266, CSE-4, G-2
Code:
#include <iostream>
using namespace std;
int main()
{
int a, b;
a = 3;
b = 5;
int choice;
cout << "Enter 1 for Swap by Value or 2 for swap by reference" << endl;
cin >> choice;
cout << "Before swapping :-" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
switch (choice)
{
case 1:
swap_value(a, b);
break;
case 2:
swap_reference(a, b);
break;
}
return 0;
}
MONISH RAI, 23/CS/266, CSE-4, G-2
Output:
MONISH RAI, 23/CS/266, CSE-4, G-2
Code:
#include <iostream>
#include <cmath>
class BankAccount {
private:
double balance;
double interestRate;
public:
BankAccount(double initialBalance, double rate) : balance(initialBalance),
interestRate(rate) {
cout << "Account created with a balance of $" << balance << " and an interest rate of "
<< interestRate << "%\n";
}
~BankAccount() {
cout << "Account closed. Final balance: $" << balance << "\n";
}
int main() {
double initialBalance, interestRate;
int userChoice;
double amount;
int years;
do {
myAccount.showMenu();
cout << "Please select an option (1-5): ";
cin >> userChoice;
switch (userChoice) {
case 1:
cout << "Enter the amount to deposit: $";
cin >> amount;
myAccount.deposit(amount);
break;
case 2:
cout << "Enter the amount to withdraw: $";
cin >> amount;
myAccount.withdraw(amount);
break;
case 3:
cout << "Enter the number of years to calculate compound interest: ";
cin >> years;
myAccount.applyCompoundInterest(years);
break;
case 4:
cout << "Your current balance is: $" << myAccount.getCurrentBalance() << "\n";
break;
case 5:
cout << "Thank you for using the Simple Banking System. Goodbye!\n";
break;
default:
cout << "Invalid option! Please choose a number between 1 and 5.\n";
}
} while (userChoice != 5);
return 0;
}
MONISH RAI, 23/CS/266, CSE-4, G-2
Output:
MONISH RAI, 23/CS/266, CSE-4, G-2
Code:
#include <iostream>
using namespace std;
class friendfunc2;
class friendfunc1 {
int num1, num2;
public:
friendfunc1(int a, int b) : num1(a), num2(b) {}
friend float calculateAverage(friendfunc1, friendfunc2);
};
class friendfunc2 {
int num3, num4, num5;
public:
friendfunc2(int c, int d, int e) : num3(c), num4(d), num5(e) {}
friend float calculateAverage(friendfunc1, friendfunc2);
};
int main() {
int a, b, c, d, e;
cout << "Enter five numbers: ";
cin >> a >> b >> c >> d >> e;
Output:
MONISH RAI, 23/CS/266, CSE-4, G-2
Code:
#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
char *str;
public:
MyString(const char *s = "")
{
str = new char[strlen(s) + 1];
strcpy(str, s);
}
~MyString()
{
delete[] str;
}
};
int main()
MONISH RAI, 23/CS/266, CSE-4, G-2
{
MyString s1("Hello"), s2(" World"), s3;
s3 = s1;
cout << "After Copy: ";
s3.display();
if (s1 == s3)
{
cout << "Strings are equal" << endl;
}
else
{
cout << "Strings are not equal" << endl;
}
MyString s4 = s1 + s2;
cout << "After Concatenation: ";
s4.display();
return 0;
}
Output:
MONISH RAI, 23/CS/266, CSE-4, G-2
Code:
#include <iostream>
using namespace std;
public:
Pair(T a, T b)
{
first = a;
second = b;
}
T get_max()
{
return (first > second) ? first : second;
}
};
int main()
{
Pair<int> intPair(10, 20);
cout << "Max (int): " << intPair.get_max() << endl;
return 0;
}
Output:
MONISH RAI, 23/CS/266, CSE-4, G-2
Code:
#include <iostream>
using namespace std;
class A
{
protected:
int a1;
public:
void set_a(int a)
{
a1 = a;
}
void display_a()
{
cout << "Class A, a1: " << a1 << endl;
}
};
class B : public A
{
protected:
int b1;
public:
void set_b(int b)
{
b1 = b;
}
void display_b()
{
cout << "Class B, b1: " << b1 << endl;
}
};
class C : public B
{
private:
int c1;
public:
void set_c(int c)
{
c1 = c;
}
void display_c()
{
cout << "Class C, c1: " << c1 << endl;
}
};
MONISH RAI, 23/CS/266, CSE-4, G-2
int main()
{
C obj;
obj.set_a(10);
obj.set_b(20);
obj.set_c(30);
obj.display_a();
obj.display_b();
obj.display_c();
return 0;
}
Output:
MONISH RAI, 23/CS/266, CSE-4, G-2
Code:
#include <iostream>
using namespace std;
class Polygon
{
public:
virtual double area() = 0;
virtual ~Polygon() {}
};
public:
Rectangle(double l, double b) : length(l), breadth(b) {}
public:
Triangle(double b, double h) : base(b), height(h) {}
int main()
{
Polygon *poly1 = new Rectangle(5.0, 3.0);
Polygon *poly2 = new Triangle(4.0, 6.0);
displayArea(poly1);
displayArea(poly2);
delete poly1;
delete poly2;
return 0;
}
MONISH RAI, 23/CS/266, CSE-4, G-2
Output: