0% found this document useful (0 votes)
31 views5 pages

Pps 2

The document defines a GreatestFinder class that finds the greatest of three numbers. It contains a constructor that initializes the numbers and a findGreatest method that compares the numbers and returns the greatest. Main creates an object, inputs three numbers, calls findGreatest on the object to get the greatest number, and prints the result.

Uploaded by

sundarrithwik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Pps 2

The document defines a GreatestFinder class that finds the greatest of three numbers. It contains a constructor that initializes the numbers and a findGreatest method that compares the numbers and returns the greatest. Main creates an object, inputs three numbers, calls findGreatest on the object to get the greatest number, and prints the result.

Uploaded by

sundarrithwik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

using namespace std;

class GreatestFinder {
private:
double num1, num2, num3;

public:
// Constructor to initialize the numbers
GreatestFinder(double n1, double n2, double n3) {
num1 = n1;
num2 = n2;
num3 = n3;
}

// Method to find the greatest number


double findGreatest() {
double greatest = num1;

if (num2 > greatest)


greatest = num2;
if (num3 > greatest)
greatest = num3;

return greatest;
}
};

int main() {
double num1, num2, num3;

// Input three numbers from user


cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;

// Create an object of GreatestFinder class


GreatestFinder finder(num1, num2, num3);

// Call the method to find the greatest number


double greatest = finder.findGreatest();

// Output the result


cout << "The greatest number is: " << greatest << endl;

return 0;
}
Q2
#include <iostream>
using namespace std;

class FactorialCalculator {
private:
int number;

public:
FactorialCalculator(int n) {
number = n;
}
long long calculateFactorial() {
if (number < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
return -1;
}
long long factorial = 1;
for (int i = 2; i <= number; ++i) {
factorial *= i;
}
return factorial;
}
};

int main() {
int num;

cout << "Enter a number to find its factorial: ";


cin >> num;

FactorialCalculator calculator(num);

long long factorial = calculator.calculateFactorial();

if (factorial != -1)
cout << "Factorial of " << num << " is: " << factorial << endl;

return 0;
}

Q3
#include <iostream>
using namespace std;

int main() {
int num, smallest1, smallest2, largest1, largest2;

cout << "Enter the number of elements: ";


cin >> num;

cout << "Enter element 1: ";


cin >> smallest1;
largest1 = smallest1;

cout << "Enter element 2: ";


cin >> smallest2;
if (smallest2 < smallest1) {
largest1 = smallest1;
smallest1 = smallest2;
} else {
largest1 = smallest2;
}

if (smallest2 > largest1) {


largest2 = smallest2;
} else {
largest2 = largest1;
largest1 = smallest2;
}

// Input the remaining numbers and update smallest1, smallest2, largest1, and largest2
for (int i = 3; i <= num; ++i) {
int temp;
cout << "Enter element " << i << ": ";
cin >> temp;

// Update smallest1 and smallest2


if (temp < smallest1) {
smallest2 = smallest1;
smallest1 = temp;
} else if (temp < smallest2) {
smallest2 = temp;
}

// Update largest1 and largest2


if (temp > largest1) {
largest2 = largest1;
largest1 = temp;
} else if (temp > largest2) {
largest2 = temp;
}
}

cout << "Two smallest elements: " << smallest1 << " and " << smallest2 << endl;
cout << "Two largest elements: " << largest1 << " and " << largest2 << endl;

return 0;
}

Q4
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Student {
private:
string name;
int rollNumber;
float cgpa;

public:
Student(string n, int roll, float cg) {
name = n;
rollNumber = roll;
cgpa = cg;
}
string getName() const {
return name;
}

int getRollNumber() const {


return rollNumber;
}

float getCGPA() const {


return cgpa;
}
};

int main() {
int numStudents;
cout << "Enter the number of students: ";
cin >> numStudents;

vector<Student> students;

for (int i = 0; i < numStudents; ++i) {


string name;
int roll;
float cg;
cout << "Enter details for student " << i + 1 << ":" << endl;
cout << "Name: ";
cin.ignore();
getline(cin, name);
cout << "Roll Number: ";
cin >> roll;
cout << "CGPA: ";
cin >> cg;
students.push_back(Student(name, roll, cg));
}

Student firstRankHolder = students[0];


for (int i = 1; i < numStudents; ++i) {
if (students[i].getCGPA() > firstRankHolder.getCGPA()) {
firstRankHolder = students[i];
}
}

cout << "\nDetails of the first rank holder:" << endl;


cout << "Name: " << firstRankHolder.getName() << endl;
cout << "Roll Number: " << firstRankHolder.getRollNumber() << endl;
cout << "CGPA: " << firstRankHolder.getCGPA() << endl;

return 0;
}

Q5
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Employee {
private:
int id;
string name;
int daysWorked;

public:
// Constructor to initialize employee details
Employee(int _id, const string& _name, int _daysWorked) : id(_id), name(_name), daysWorked(_daysWorked) {}
// Getter methods to access private member variables
int getID() const { return id; }
const string& getName() const { return name; }
int getDaysWorked() const { return daysWorked; }
float getTotalSalary() const { return daysWorked * 1000; } // Assuming salary is 1000 rupees per day
};

int main() {
int numEmployees;

cout << "Enter the number of employees: ";


cin >> numEmployees;

vector<Employee> employees;

for (int i = 0; i < numEmployees; ++i) {


int id, daysWorked;
string name;

cout << "\nEnter details for employee " << i + 1 << ":" << endl;
cout << "Employee ID: ";
cin >> id;
cout << "Employee Name: ";
cin.ignore();
getline(cin, name);
cout << "Number of Days Worked: ";
cin >> daysWorked;

employees.emplace_back(id, name, daysWorked);


}

cout << "\nDetails of all employees:" << endl;


for (int i = 0; i < numEmployees; ++i) {
const Employee& emp = employees[i];
cout << "\nEmployee " << i + 1 << ":" << endl;
cout << "ID: " << emp.getID() << endl;
cout << "Name: " << emp.getName() << endl;
cout << "Number of Days Worked: " << emp.getDaysWorked() << endl;
cout << "Total Salary: " << emp.getTotalSalary() << " rupees" << endl;
}

return 0;
}

You might also like