EUROPEAN UNIVERSITY OF LEFKE
Faculty of Engineering
Department of Computer Engineering
COMP 218
OBJECT-ORIENTED PROGRAMMING
Lab Work No. 2
Prepared by Kalsoom Shah (194371 )
Submitted to Mr. Salman Khan
Question No 1:
Task (a)
Asks user to specify five floating-point values, adds them, and then displays the
result to the console screen
Solution:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float a,b,c,d,e,sum;
cout<<"Enter the first float value:";
cin>>a;
cout<<"Enter the second float value:";
cin>>b;
cout<<"Enter the third float value:";
cin>>c;
cout<<"Enter the fourth float value:";
cin>>d;
cout<<"Enter the fifth float value:";
cin>>e;
sum=a+b+c+d+e;
cout << "The sum of your floating values are:" << sum<< endl;
return 0;
}
Task (b)
Asks user to specify five integers, finds the smallest one, and then displays the result
to the console screen
Solution:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float a,b,c,d,e,smallest;
cout<<"Enter the first integer value:";
cin>>a;
cout<<"Enter the second integer value:";
cin>>b;
cout<<"Enter the third integer value:";
cin>>c;
cout<<"Enter the fourth integer value:";
cin>>d;
cout<<"Enter the fifth integer value:";
cin>>e;
if(a<b && a<c && a<d && a<e)
{
smallest=a;
}
else if(b<c && b<d && b<e && b<a)
{
smallest=b;
}
else if (c<d && c<e && c<b && c<a)
{
smallest=c;
}
else if(d<e && d<a && d<b && d<c)
{
smallest=d;
}
else
{
smallest =e;
}
cout << "The smallest value among all values are " <<smallest << endl;
return 0;
}
Task (C)
Calculates the power of n raised by m, where n and m are user-specified values
Solution:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
float a,b,power;
cout<<"Enter the base value:";
cin>>a;
cout<<"Enter the exponent value:";
cin>>b;
power = pow(a,b);
cout << "The final results are: " <<power << endl;
return 0;
}
Question No 2:
Task (a)
Solution:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
int choice;
double a,b,result;
while (true) {
cout << "Menu:" << endl;
cout << "1. Add" << endl;
cout << "2. Subtract" << endl;
cout << "3. Multiply" << endl;
cout << "4. Quit" << endl;
cout << "Enter your choice (1-4):\n";
cout << "You can only write two numbers to perform any action:\n";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
result = a + b;
cout << "Result: " << result << endl;
break;
case 2:
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
result = a - b;
cout << "Result: " << result << endl;
break;
case 3:
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
result = a * b;
cout << "Result: " << result << endl;
break;
case 4:
cout << "Exit" << endl;
return 0;
default:
cout << "Invalid choice. Please enter a number between 1 and 4." << endl;
}
}
return 0;
}
Task (b)
Solution:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
char choice;
double a,b,result;
while (true) {
cout << "Menu:" << endl;
cout << "+. Add" << endl;
cout << "-. Subtract" << endl;
cout << "*. Multiply" << endl;
cout << ". Quit" << endl;
cout << "Enter your choice (+, -, *, .):\n";
cout << "You can only write two numbers to perform any action:\n";
cin >> choice;
switch (choice) {
case '+':
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
result = a + b;
cout << "Result: " << result << endl;
break;
case '-':
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
result = a - b;
cout << "Result: " << result << endl;
break;
case '*':
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
result = a * b;
cout << "Result: " << result << endl;
break;
case '.':
cout << "Exit" << endl;
return 0;
default:
cout << "Invalid choice. Please enter a number between 1 and 4." << endl;
}
}
return 0;
}