0% found this document useful (0 votes)
18 views12 pages

Lab Work No. 2: European University of Lefke Faculty of Engineering Department of Computer Engineering

Uploaded by

Academic Hub
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)
18 views12 pages

Lab Work No. 2: European University of Lefke Faculty of Engineering Department of Computer Engineering

Uploaded by

Academic Hub
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/ 12

EUROPEAN UNIVERSITY OF LEFKE

Faculty of Engineering
Department of Computer Engineering

COMP 218 OBJECT-ORIENTED PROGRAMMING

Lab Work No. 2

Prepared by Lucia Moyo (184826)

Submitted to Mr. Salman Khan


1a. #include <iostream>

int main() {

float values[5];

float sum = 0;

std::cout << "Enter the 5 floating-point values:\n";

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

std::cout << "Value " << i + 1 << ": ";

std::cin >> values[i];

sum += values[i];

std::cout << "Sum of the values: " << sum << std::endl;

return 0;

1b. #include <iostream>

#include <iomanip>

int main() {

int integers[5];
int smallest;

std::cout << "Enter five integers:\n";

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

std::cout << "Integer " << i + 1 << ": ";

std::cin >> integers[i];

smallest = integers[0];

for (int i = 1; i < 5; ++i) {

if (integers[i] < smallest) {

smallest = integers[i];

std::cout << "Smallest integer: " << smallest << std::endl;

return 0;

1c. #include <iostream>

#include <cmath>

#include <iomanip>
int main() {

int n, m;

double result;

std::cout << "Enter integers n and m to calculate n^m:\n";

std::cout << "n: ";

std::cin >> n;

std::cout << "m: ";

std::cin >> m;

result = std::pow(n, m);

std::cout << n << "^" << m << " = " << result << std::endl;

return 0;

2a. #include <iostream>

#include <iomanip>

int main() {

char choice;

float num1, num2, result;


do {

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

std::cout << "1. Add\n";

std::cout << "2. Exit\n";

std::cout << "Enter the number of your choice: ";

std::cin >> choice;

switch(choice) {

case '5':

std::cout << "Enter first number: ";

std::cin >> num1;

std::cout << "Enter second number: ";

std::cin >> num2;

result = num1 + num2;

std::cout << "Result: " << result << std::endl;

break;

case '6':

std::cout << "Exiting program...\n";

break;

default:

std::cout << "Invalid choice. Please try again.\n";

} while(choice != '2');

return 0;

}
2b. #include <iostream>

#include <iomanip>

int main() {

char choice;

float num1, num2, result;

do {

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

std::cout << "1. Subtract\n";

std::cout << "2. Exit\n";

std::cout << "Enter your choice: ";

std::cin >> choice;

switch(choice) {

case '3':

std::cout << "Enter first number: ";

std::cin >> num1;

std::cout << "Enter second number: ";

std::cin >> num2;

result = num1 - num2;

std::cout << "Result: " << result << std::endl;

break;

case '4':
std::cout << "Exiting program...\n";

break;

default:

std::cout << "Invalid choice. Please try again.\n";

} while(choice != '2');

return 0;

2c. #include <iostream>

#include <iomanip>

int main() {

char choice;

float num1, num2, result;

do {

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

std::cout << "1. Multiply\n";

std::cout << "2. Exit\n";

std::cout << "Enter your choice: ";

std::cin >> choice;

switch(choice) {

case '7':

std::cout << "Enter first number: ";


std::cin >> num1;

std::cout << "Enter second number: ";

std::cin >> num2;

result = num1 * num2;

std::cout << "Result: " << result << std::endl;

break;

case '8':

std::cout << "Exiting program...\n";

break;

default:

std::cout << "Invalid choice. Please try again.\n";

} while(choice != '2');

return 0;

2d. #include <iostream>

#include <iomanip>

int main() {

char choice;

float num1, num2, result;

do {

//

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


std::cout << "1. Perform Addition\n";

std::cout << "2. Perform Subtraction\n";

std::cout << "3. Quit\n";

std::cout << "Enter your choice (1-3): ";

std::cin >> choice;

switch(choice) {

case '1':

std::cout << "Enter first number: ";

std::cin >> num1;

std::cout << "Enter second number: ";

std::cin >> num2;

result = num1 + num2;

std::cout << "Result of addition: " << result << std::endl;

break;

case '2':

std::cout << "Enter first number: ";

std::cin >> num1;

std::cout << "Enter second number: ";

std::cin >> num2;

result = num1 - num2;

std::cout << "Result of subtraction: " << result << std::endl;

break;

case '3':

std::cout << "Quitting program...\n";

break;

default:

std::cout << "Invalid choice. Please try again.\n";


}

} while(choice != '3');

return 0;

3. #include <iostream>

#include <iomanip>

int main() {

char choice;

float num1, num2, result;

do {

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

std::cout << "+. Add\n";

std::cout << "-. Subtract\n";

std::cout << "*. Multiply\n";

std::cout << ". Quit\n";

std::cout << "Enter your choice: ";

std::cin >> choice;

switch(choice) {

case '+':
std::cout << "Enter first number: ";

std::cin >> num1;

std::cout << "Enter second number: ";

std::cin >> num2;

result = num1 + num2;

std::cout << "Result: " << std::fixed << std::setprecision(2) << result << std::endl; // Formatting the
output

break;

case '-':

std::cout << "Enter first number: ";

std::cin >> num1;

std::cout << "Enter second number: ";

std::cin >> num2;

result = num1 - num2;

std::cout << "Result: " << std::fixed << std::setprecision(2) << result << std::endl; // Formatting the
output

break;

case '*':

std::cout << "Enter first number: ";

std::cin >> num1;

std::cout << "Enter second number: ";

std::cin >> num2;

result = num1 * num2;

std::cout << "Result: " << std::fixed << std::setprecision(2) << result << std::endl; // Formatting the
output

break;

case '.':

std::cout << "Quitting program...\n";


break;

default:

std::cout << "Invalid choice. Please try again.\n";

} while(choice != '.');

return 0;

You might also like