COMP 218: Lab Work No. 1
COMP 218: Lab Work No. 1
COMP 218: Lab Work No. 1
Faculty of Engineering
Department of Computer Engineering
COMP 218
OBJECT-ORIENTED
PROGRAMMING
a.
#include <iostream>
int main() {
float num1, num2, num3, num4, num5;
std::cout << "Enter five floating-point values:\n";
std::cin >> num1 >> num2 >> num3 >> num4 >> num5;
float sum = num1 + num2 + num3 + num4 + num5;
std::cout << "The sum of the five values is: " << sum << std::endl;
return 0;
}
1
b.
#include <iostream>
#include <limits>
int main() {
int integers[5];
std::cout << "Please enter five integers: ";
for (int i = 0; i < 5; ++i) {
std::cin >> integers[i];
}
int smallest = std::numeric_limits<int>::max();
for (int i = 0; i < 5; ++i) {
if (integers[i] < smallest) {
smallest = integers[i];
}
}
std::cout << "The smallest integer is: " << smallest << std::endl;
return 0;
}
c.
#include <iostream>
double power(double base, int exponent) {
double result = 1.0;
for (int i = 0; i < exponent; ++i) {
result *= base;
}
return result;
}
int main() {
double m;
int n;
std::cout << "Enter the base (m): ";
std::cin >> m;
std::cout << "Enter the exponent (n): ";
std::cin >> n;
double result = power(m, n);
std::cout << m << " raised to the power of " << n << " is: " << result <<
std::endl;
return 0;
}
Task-2:
#include <iostream>
do {
switch (choice) {
case 1:
std::cout << "Enter two numbers to add: ";
std::cin >> num1 >> num2;
std::cout << "Result: " << add(num1, num2) << std::endl;
break;
case 2:
std::cout << "Enter two numbers to subtract: ";
std::cin >> num1 >> num2;
std::cout << "Result: " << subtract(num1, num2) << std::endl;
break;
case 3:
std::cout << "Enter two numbers to multiply: ";
std::cin >> num1 >> num2;
std::cout << "Result: " << multiply(num1, num2) << std::endl;
break;
case 4:
std::cout << "Quitting program. Goodbye!\n";
break;
default:
std::cout << "Invalid choice. Please enter a number between 1 and
4.\n";
}
} while (choice != 4);
return 0;
}
TASK-3:
#include <iostream>
int main() {
char choice;
float num1, num2;
do {
// Display menu
cout << "Menu:\n";
cout << "+. Add\n";
cout << "-. Subtract\n";
cout << "*. Multiply\n";
cout << ". Quit\n";
cout << "Enter your choice (+, -, *, .): ";
cin >> choice;
return 0;
}