Auditory Exercises 3
Auditory Exercises 3
Auditory Exercises 3
Structural Programming
Tutorial 3
1. Operators in C++
1.1. Relational Operators
They can be applied to any comparable data types, and the result is an integer: 0 (false) or 1
(true).
Operator Meaning
== Equality
!= Inequality (difference)
They are most commonly used in combination with relational operators to form complex logical
expressions, which again return a result of 0 or 1.
Operator Meaning
Operator
|| Meaning
Logical OR
! Negation (NOT)
Example:
int a = 5 && 0; // a = 0;
a = 2 && 5; // a = 1;
a = 0 || 5; // a = 1;
a = !0; // a = 1;
a = !5; // a = 0;
x = (y = 10) * (z = 5);
x = y = z = 20;
a. Prefix
The value of the variable is increased before the expression in which it participates is calculated.
a = ++b;
b. Postfix
a = b++;
https://finki-mk.github.io/SP_2023/en/3.html 2/10
10/21/23, 7:45 AM 3
+= operator
a += 5; // a = a + 5;
a += b * c; // a = a + b * c;
-= operator
a -= 3; // a = a - 3;
*= operator
a *= 3; // a = a * 3;
/= operator
a /= 3; // a = a / 3;
%= operator
a %= 3; // a = a % 3;
https://finki-mk.github.io/SP_2023/en/3.html 3/10
10/21/23, 7:45 AM 3
if (condition) {
statements_if_true;
}
else {
statements_if_false;
}
#include <iostream>
using namespace std;
int main() {
int m = 5, n = 10;
if (m > n)
++m;
++n;
cout << "m = " << m << ", n = " << n;
return 0;
}
Solution: m = 5, n = 11
https://finki-mk.github.io/SP_2023/en/3.html 4/10
10/21/23, 7:45 AM 3
3. Exercises
3.1. Exercise 1
Write a program that reads a character from the keyboard and prints 1 if it is a lowercase letter or 0
if it is an uppercase letter.
Solution:
#include <iostream>
using namespace std;
int main()
{
char ch;
int result;
cout << "Enter a character: ";
cin >> ch;
result = (ch >= 'a' && ch <= 'z');
cout << result;
return 0;
}
Bonus solution:
3.2. Exercise 2
Write a program that reads two integers (x and y) from the keyboard and prints the result (z) of the
following expression:
Solution:
#include <iostream>
using namespace std;
https://finki-mk.github.io/SP_2023/en/3.html 5/10
10/21/23, 7:45 AM 3
int main()
{
int x, y, z;
cout << "Enter values for x and y: ";
cin >> x >> y;
z = x++ + --y + (x < y);
cout << z;
return 0;
}
3.3. Exercise 3
a. Given:
What will be the value of r for x = 1, y = 2, and z = 3? What will be the value of z?
b. Given:
What will be the value of r for x = 1, y = 2, and z = 3? What will be the value of z?
Solution:
a. r = 1, z = 3
b. r = 0, z = 3
3.4. Exercise 4
Write a program that reads the price of a product from the keyboard and then prints its price with
added value-added tax (VAT).
Solution:
#include <iostream>
using namespace std;
https://finki-mk.github.io/SP_2023/en/3.html 6/10
10/21/23, 7:45 AM 3
int main()
{
float price;
cout << "Enter the product price: ";
cin >> price;
cout << "The total price of the product is " << price * 1.18;
return 0;
}
3.5. Exercise 5
Write a program that reads the price of a product, the number of installments, and the interest rate
(the interest rate is a number expressed as a percentage from 0 to 100). The program should print
the installment amount and the total amount to be paid for the product.
Solution:
#include <iostream>
using namespace std;
int main()
{
float price, interestRate, installment, total;
int installments;
cout << "Enter the product price: " << endl;
cin >> price;
cout << "Enter the number of installments: ";
cin >> installments;
cout << "Enter the interest rate: ";
cin >> interestRate;
total = price * (1 + interestRate / 100);
installment = total / installments;
cout
<< "One installment will be: " << installment << endl;
cout << "The total amount paid will be: " << total;
return 0;
}
3.6. Exercise 6
https://finki-mk.github.io/SP_2023/en/3.html 7/10
10/21/23, 7:45 AM 3
Write a program that reads a three-digit integer from the keyboard. The program should print the
most significant and least significant digits of the number.
Example: If you enter the number 795, the program will print: The most significant digit is 7, and
the least significant is 5.
Solution:
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter the number: ";
cin >> number;
cout << "The most significant digit is " << (number / 100);
cout << ", and the least significant digit is " << (number % 10);
return 0;
}
3.7. Exercise 7
Write a program that reads a date in the format (ddmmyyyy) from the keyboard. The program
should print the day and the month of the birthdate.
Example: If you enter the number 18091992, the program will print: 18.9
Solution:
#include <iostream>
using namespace std;
int main()
{
long int date;
int day, month;
cout << "Enter your birthdate: ";
cin >> date;
https://finki-mk.github.io/SP_2023/en/3.html 8/10
10/21/23, 7:45 AM 3
3.8. Exercise 8
Write a program that will print the maximum of two numbers whose values are read from the
keyboard.
Solution:
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter 2 numbers: " << endl;
cin >> a >> b;
if (a > b)
cout << "Maximum: " << a;
else
cout << "Maximum: " << b;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, max;
cout << "Enter 2 numbers: " << endl;
cin >> a >> b;
max = (a > b) ? a : b;
cout << "Maximum: " << max;
return 0;
}
3.9. Exercise 9
https://finki-mk.github.io/SP_2023/en/3.html 9/10
10/21/23, 7:45 AM 3
Write a program that checks if a given year, read from the keyboard, is a leap year or not and
prints an appropriate message.
Hint: A year is a leap year if it is divisible by 4 but not divisible by 100, or if it is divisible by 400.
Solution:
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: " << endl;
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
cout << year << " is a leap year." << endl;
else
cout << year << " is not a leap year." << endl;
return 0;
}
https://finki-mk.github.io/SP_2023/en/3.html 10/10