European University of Lefke Faculty of Engineering Department of Computer Engineering

You might also like

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. 2

Prepared by Godwin Anyaso (194267)

Submitted to Mr Salman Khan


Task 1A:
#include <iostream>

int main()
{
using namespace std;
float num1, num2, num3, num4, num5;
cout << "Enter Num1: ";
cin >> num1;//Takes input for num1
cout << "Enter Num2: ";
cin >> num2;//Takes input for num2
cout << "Enter Num3: ";
cin >> num3;//Takes input for num3
cout << "Enter Num4: ";
cin >> num4;//Takes input for num4
cout << "Enter Num5: ";
cin >> num5;//Takes input for num5
cout<<"Total is " << num1+num2+num3+num4+num5 << endl;

return 0;
}
Task 1b:
#include <iostream>
#include <climits>

int main() {
using namespace std;

int num1, num2, num3, num4, num5;


int smallest = INT_MAX; // Initialize smallest to maximum possible integer value

cout << "Enter Num1: ";


cin >> num1;//Takes input for num1
cout << "Enter Num2: ";
cin >> num2;//Takes input for num2
cout << "Enter Num3: ";
cin >> num3;//Takes input for num3
cout << "Enter Num4: ";
cin >> num4;//Takes input for num4
cout << "Enter Num5: ";
cin >> num5;//Takes input for num5

// Iterate through five numbers to find the smallest number among the five
if (num1 < smallest) {
smallest = num1;
}
if (num2 < smallest) {
smallest = num2;
}
if (num3 < smallest) {
smallest = num3;
}
if (num4 < smallest) {
smallest = num4;
}
if (num5 < smallest) {
smallest = num5;
}

cout << "The smallest integer is: " << smallest << endl;

return 0;
}

Task 1c:
#include <iostream>
#include <cmath> // for pow function

int main() {
using namespace std;

double n, m;

// Getting user input for n and m


cout << "Enter the base (n): ";
cin >> n;

cout << "Enter the exponent (m): ";


cin >> m;

// Calculating power of n raised by m


double result = pow(n, m);

// Displaying the result


cout << n << " raised to the power of " << m << " is: " << result << endl;

return 0;
}

Task-2: Write a menu-driven C++ program that performs the following according to
user’s choice

#include <iostream>

int main() {
using namespace std;

int choice;
float num1, num2, result;
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: ";
cin >> num1 >> num2;
result = num1 + num2;
cout << "Result: " << result << endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 - num2;
cout << "Result: " << result << endl;
break;
case 3:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = num1 * num2;
cout << "Result: " << result << endl;
break;
case 4:
cout << "Quitting the program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please choose between 1 and 4." << endl;
}

return 0;
}
Task 3: Rewrite the program written for Task-2 in which the user can enter symbols ‘+’,
‘-’, ‘*’, and ‘.’ for quitting

#include <iostream>

int main() {
using namespace std;

char choice;
float num1, num2, result;

cout << "Welcome to the Simple Calculator" << endl;


cout << "Enter '+' for addition, '-' for subtraction, '*' for multiplication, or '.' to
quit" << endl;

cout << "Enter your choice: ";


cin >> choice;

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

return 0;
}

You might also like