European University of Lefke Faculty of Engineering Department of Computer Engineering
European University of Lefke Faculty of Engineering Department of Computer Engineering
European University of Lefke Faculty of Engineering Department of Computer Engineering
Faculty of Engineering
Department of Computer Engineering
COMP 218
OBJECT-ORIENTED PROGRAMMING
int main()
{
using namespace std;
float num1, num2, num3, num4, num5;
cout << "Enter Num1: ";
cin >> num1;//Takes input for num1
cout << "Enter Num2: ";
cin >> num2;//Takes input for num2
cout << "Enter Num3: ";
cin >> num3;//Takes input for num3
cout << "Enter Num4: ";
cin >> num4;//Takes input for num4
cout << "Enter Num5: ";
cin >> num5;//Takes input for num5
cout<<"Total is " << num1+num2+num3+num4+num5 << endl;
return 0;
}
Task 1b:
#include <iostream>
#include <climits>
int main() {
using namespace std;
// Iterate through five numbers to find the smallest number among the five
if (num1 < smallest) {
smallest = num1;
}
if (num2 < smallest) {
smallest = num2;
}
if (num3 < smallest) {
smallest = num3;
}
if (num4 < smallest) {
smallest = num4;
}
if (num5 < smallest) {
smallest = num5;
}
cout << "The smallest integer is: " << smallest << endl;
return 0;
}
Task 1c:
#include <iostream>
#include <cmath> // for pow function
int main() {
using namespace std;
double n, m;
return 0;
}
Task-2: Write a menu-driven C++ program that performs the following according to
user’s choice
#include <iostream>
int main() {
using namespace std;
int choice;
float num1, num2, result;
cout << "1. Add\n2. Subtract\n3. Multiply\n4. Quit" << endl;
switch(choice) {
case 1:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 + num2;
cout << "Result: " << result << endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 - num2;
cout << "Result: " << result << endl;
break;
case 3:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 * num2;
cout << "Result: " << result << endl;
break;
case 4:
cout << "Quitting the program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please choose between 1 and 4." << endl;
}
return 0;
}
Task 3: Rewrite the program written for Task-2 in which the user can enter symbols ‘+’,
‘-’, ‘*’, and ‘.’ for quitting
#include <iostream>
int main() {
using namespace std;
char choice;
float num1, num2, result;
switch(choice) {
case '+':
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 + num2;
cout << "Result: " << result << endl;
break;
case '-':
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 - num2;
cout << "Result: " << result << endl;
break;
case '*':
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 * num2;
cout << "Result: " << result << endl;
break;
case '.':
cout << "Quitting the program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please enter '+', '-', '*', or '.'" << endl;
}
return 0;
}