You are on page 1of 6

EUROPEAN UNIVERSITY OF LEFKE

Faculty of Engineering
Department of Computer Engineering

COMP218
OBJECT-ORIENTED PROGRAMMING

Lab Work No. 2

Prepared by Amr Ali Abdelsayed Wadan (184649)

Submitted to Dr. Ferhun Yorgancıoğlu


Task (1)

a)
#include <iostream> using namespace std; int main() { float
array[5], sum = 0; //declare variables cout << "Enter Five
Floating Point Values: "; for (int i = 0; i < 5; i++)
{
cin >> array[i]; //read array
sum = sum + array[i]; // Calculating Sum
}
cout << "Result = " << sum;
}

1
b)
#include <iostream> using namespace std; int
main() { float array[5]; cout << "Enter
Five Floating Point Values: "; for (int i =
0; i < 5; i++)
{
cin >> array[i]; //read array
}
float min = array[0]; for (int i = 1; i < 5; i++)
// Calculating Smallest One
{
if (array[i] < min)
{
min = array[i];
}
} cout << "Result = " << min; //Printing the
Result
}

2
c)
#include <iostream>
using namespace std; int
main()
{
int n, m; cout
<< "Enter n = "; cin
>> n; //read n cout
<< "Enter m = "; cin
>> m; //read m int power
= 1; for (int i = 1; i <=
m; i++)
{
power = power * n; //Calculating Power
}
cout << "Result = " << power; //Print the Result
}

3
Task (2)
# include <iostream> using namespace std; int main() { char op;
float num1, num2; cout <<
"1:Add\n2:Subtract\n3:Multiply\n4:Quit\nChoose a number:
"; cin >> op;
cout << "Enter two values: ";
cin >> num1 >> num2; switch
(op) { case '1':
cout << num1 + num2; break;
case '2': cout << num1 -
num2; break; case '3':
cout << num1 * num2; break;
case
'4': cout <<
"Goodbye.\n"; op = 4; break;
default:
// If the Number other than 1, 2, 3 or 4, error message is
shown
cout << "Error! operator is not correct";
break; } return 0;
}

Task (3)
# include <iostream>
using namespace std; int
main() { char op;
float num1,

4
num2; cout << "+ for Adding\n- for Subtracting\n*
Multiplying\n. for
Quitting\nChoose a symbol: ";
cin >> op; cout << "Enter two
values: "; cin >> num1 >> num2;
switch (op) { case '+':
cout << num1 + num2; break;
case '-': cout << num1 -
num2; break; case '*':
cout << num1 * num2; break;
case '.': cout <<
"Goodbye.\n"; break; default:
// If the symbols are not +, -, * or ., error message is
shown cout << "Error! Symbol is not correct";
break; } return 0;
}

You might also like