COMP 218: Lab Work No. 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

EUROPEAN UNIVERSITY OF LEFKE

Faculty of Engineering
Department of Computer Engineering

COMP 218
OBJECT-ORIENTED
PROGRAMMING

Lab Work No. 1


Prepared by Elif Su Çinkay (20120085)

Submitted to Mr. Salman Khan


Task-1:

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>

double add(double a, double b) {


return a + b;
}

double subtract(double a, double b) {


return a - b;
}

double multiply(double a, double b) {


return a * b;
}
int main() {
int choice;
double num1, num2;

do {

std::cout << "Menu:\n"


"1. Add\n"
"2. Subtract\n"
"3. Multiply\n"
"4. Quit\n"
"Enter your choice (1-4): ";
std::cin >> choice;

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>

using namespace std;

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;

// Perform operation based on choice


switch (choice) {
case '+':
cout << "Enter two numbers to add: ";
cin >> num1 >> num2;
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Enter two numbers to subtract: ";
cin >> num1 >> num2;
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Enter two numbers to multiply: ";
cin >> num1 >> num2;
cout << "Result: " << num1 * num2 << endl;
break;
case '.':
cout << "Quitting program. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please enter +, -, *, or .\n";
}
} while (choice != '.');

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy