0% found this document useful (0 votes)
54 views6 pages

COMP 218: Lab Work No. 2

The document contains code for 3 programming tasks. Task 1 involves (a) calculating the sum of 5 floating point numbers, (b) finding the minimum of 5 integers, and (c) calculating powers. Task 2 is a menu-driven calculator. Task 3 is a similar calculator that uses symbols instead of numbers for choices.

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)
54 views6 pages

COMP 218: Lab Work No. 2

The document contains code for 3 programming tasks. Task 1 involves (a) calculating the sum of 5 floating point numbers, (b) finding the minimum of 5 integers, and (c) calculating powers. Task 2 is a menu-driven calculator. Task 3 is a similar calculator that uses symbols instead of numbers for choices.

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/ 6

EUROPEAN UNIVERSITY OF LEFKE

Faculty of Engineering
Department of Software Engineering

COMP 218
OBJECT-ORIENTED PROGRAMMING

Lab Work No. 2

Prepared by İsmail FALAY (181354)

Submitted to Mr. Salman Khan


// TASK1
// a
#include <iostream>
using namespace std;

int main() {
cout << "Task-1a: Enter five floating-point values:" << endl;
double sum = 0;
for (int i = 0; i < 5; ++i) {
double value;
cout << "Enter value " << (i + 1) << ": ";
cin >> value;
sum += value;
}
cout << "Sum of the values is: " << sum << endl;

return 0;
}

// TASK1
// b

#include <iostream>
#include <climits>
using namespace std;

int main() {
cout << "Task-1b: Enter five integers:" << endl;
int minInt = INT_MAX;
for (int i = 0; i < 5; ++i) {
int intValue;
cout << "Enter integer " << (i + 1) << ": ";
cin >> intValue;
if (intValue < minInt) {
minInt = intValue;
}
}
cout << "The smallest integer is: " << minInt << endl;

return 0;
}
// TASK1
// c
#include <iostream>
#include <cmath>
using namespace std;

double calculatePower(double n, int m) {


return pow(n, m);
}

int main() {
cout << "Task-1c: Enter values for n and m to calculate n^m:" << endl;
double n, result;
int m;
cout << "Enter value for n: ";
cin >> n;
cout << "Enter value for m: ";
cin >> m;
result = calculatePower(n, m);
cout << n << " raised to the power of " << m << " is: " << result << endl;

return 0;
}

// TASK2

#include <iostream>
using namespace std;

int main() {
int choice;
double num1, num2, result;

cout << "Task-2: Menu-Driven Calculator" << endl;


cout << "1. Add\n2. Subtract\n3. Multiply\n4. Quit" << endl;
cout << "Enter your choice (1-4): ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter two numbers to add:" << endl;
cin >> num1 >> num2;
result = num1 + num2;
cout << "Sum: " << result << endl;
break;

case 2:
cout << "Enter two numbers to subtract:" << endl;
cin >> num1 >> num2;
result = num1 - num2;
cout << "Difference: " << result << endl;
break;

case 3:
cout << "Enter two numbers to multiply:" << endl;
cin >> num1 >> num2;
result = num1 * num2;
cout << "Product: " << result << endl;
break;

case 4:
cout << "Quitting the program." << endl;
break;

default:
cout << "Invalid choice. Please enter a number between 1 and 4." << endl;
}

return 0;
}
// TASK3
#include <iostream>
using namespace std;

int main() {
char symbol;
double num1, num2, result;

cout << "Task-3: Menu-Driven Calculator with Symbols" << endl;


cout << "+. Add\n-. Subtract\n*. Multiply\n.. Quit" << endl;
cout << "Enter your choice (+, -, *, .): ";
cin >> symbol;

switch (symbol) {
case '+':
cout << "Enter two numbers to add:" << endl;
cin >> num1 >> num2;
result = num1 + num2;
cout << "Sum: " << result << endl;
break;

case '-':
cout << "Enter two numbers to subtract:" << endl;
cin >> num1 >> num2;
result = num1 - num2;
cout << "Difference: " << result << endl;
break;

case '*':
cout << "Enter two numbers to multiply:" << endl;
cin >> num1 >> num2;
result = num1 * num2;
cout << "Product: " << result << endl;
break;

case '.':
cout << "Quitting the program." << endl;
break;

default:
cout << "Invalid choice. Please enter one of the specified symbols." << endl;
}

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