Structural Programming

Auditory exercise 2


1. Introduction to C++

2. Program Structure

Reminder: The source code of the C++ programming language is organized into functions.

#include<iostream>
using namespace std;

int main() {
  variable declaration;
  program statements;
}

3. Basic C++ Program

#include <iostream>
using namespace std;
// main function
int main() {
  /*
  print messages to the screen
  */
  cout << "Welcome to FINKI!" << endl;
  return 0;
}

4. Input/Output Streams in C++ (<iostream>)

In C++, for working with input/output streams, we use the operators: - << from the cout object (cout <<) - >> from the cin object (cin >>)

These objects are part of the standard library iostream, which is included in the program using the directive #include <iostream> at the beginning of the code.

Example:

#include<iostream>
using namespace std;

int main() {
   int value;
   cout << "Enter a value for the variable value: ";
   cin >> value;
   cout << "The entered value is: " << value << '\n';
}

5. Variables

5.1. Variable Declaration in C++

In the C++ programming language, variables are declared with the following syntax:

data_type variable_name = initial_value;

Examples of variable declarations of different types:

int number = 5;
float price = 7.99;
char letter = 'a';
bool is_true = true;

5.2. Variable Types in C++

Integer Character Floating-Point Boolean
int char float bool
short double
long long double

6. Comments

Comments are used for explanations or documentation within the source code. There are two types of comments:

// This is a single-line comment
/*
This is a
multi-line comment
*/

7. Operators

Operators are applied to numbers (integers or decimals):

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder when dividing)

8. Exercises

8.1. Exercise 1

Write a program that calculates the value of the mathematical expression: x = 3/2 + (5 - 46*5/12)

Solution:

#include <iostream>
using namespace std;

int main() {
    float x = 3.0 / 2 + (5 - 46 * 5.0 / 12);
    cout << "x = " << x << endl;
    return 0;
}

8.2. Exercise 2

Write a program that, given a value of x (as a variable declaration), calculates and prints x squared.

Solution:

#include <iostream>
using namespace std;

int main() {
    int x = 7;
    cout << "The square of " << x << " is " << x * x << endl;
    return 0;
}

8.3. Exercise 3

Write a program that, for given sides of an equilateral triangle, prints its perimeter and the square of its area (let’s use a = 5, b = 7.5, c = 10.2).

Solution:

#include <iostream>
using namespace std;

int main() {
    float a = 5.0;
    float b = 7.5;
    float c = 10.2;
    float perimeter = a + b + c;
    float s = perimeter / 2;
    float area = s * (s - a) * (s - b) * (s - c);
    cout << "The perimeter is: " << perimeter << endl;
    cout << "The area square is: " << area << endl;
    return 0;
}

8.4. Exercise 4

Write a program that calculates the arithmetic mean of the numbers 3, 5, and 12.

Solution:

#include <iostream>
using namespace std;

int main() {
    int a = 3;
    int b = 5;
    int c = 12;
    float average = (a + b + c) / 3.0

;
    cout << "The arithmetic mean is: " << average << endl;
    return 0;
}

8.5. Exercise 5

Write a program that prints the remainders when the number 19 is divided by 2, 3, 5, and 8.

Solution:

#include <iostream>
using namespace std;

int main() {
    int num = 19;
    cout << "The remainder when 19 is divided by 2 is: " << num % 2 << endl;
    cout << "The remainder when 19 is divided by 3 is: " << num % 3 << endl;
    cout << "The remainder when 19 is divided by 5 is: " << num % 5 << endl;
    cout << "The remainder when 19 is divided by 8 is: " << num % 8 << endl;
    return 0;
}

8.6. Exercise 6

Write a program to calculate and print the area and perimeter of a circle. The radius of the circle should be read from the standard input (keyboard) as a decimal number.

Solution:

#include <iostream>
using namespace std;

int main() {
    float radius;
    cin >> radius;

    float perimeter = 2 * radius * 3.14;
    float area = radius * radius * 3.14;

    cout << "Perimeter = " << perimeter << endl;
    cout << "Area = " << area << endl;
    return 0;
}

8.7. Exercise 7

Write a program that reads two integers from the standard input and prints their sum, difference, product, and remainder when divided.

Solution:

#include <iostream>
using namespace std;

int main() {
    int x, y;
    cin >> x >> y;

    cout << x << " + " << y << " = " << x + y << endl;
    cout << x << " - " << y << " = " << x - y << endl;
    cout << x << " * " << y << " = " << x * y << endl;
    cout << x << " % " << y << " = " << x % y << endl;
    return 0;
}

8.8. Exercise 8

Write a program that reads an uppercase letter from the standard input and prints the same letter as lowercase. - Note: Each character is represented by an ASCII number.

Solution:

#include <iostream>
using namespace std;

int main() {
    char uppercase;
    cout << "Enter an uppercase letter: " << endl;
    cin >> uppercase;
    cout << uppercase << " is written in lowercase as " << char(uppercase + ('a' - 'A')) << endl;
    return 0;
}

8.9. Exercise 9

Write a program that reads the price of a product from the keyboard and then prints its price with added value-added tax (VAT).

Hint: VAT is 18% of the initial price.

Solution:

#include <iostream>
using namespace std;

int main()
{
    float price;
    cout << "Enter the product price: ";
    cin >> price;
    cout << "The total price of the product is " << price * 1.18;
    return 0;
}

8.10. Exercise 10

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.

Hint: Calculate the total amount and then the installment.

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;
}

8.11. Exercise 11

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.

Hint: Use integer division and the modulo operator.

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;
}

8.12. Exercise 12

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

Hint: Use integer division and the modulo operator.

Solution:

#include <iostream>
using namespace std;

int main()
{
    long int date;
    int day, month;
    cout << "Enter your birthdate: ";
    cin >> date;
    day = date / 1000000;
    month = (date / 10000) % 100;
    cout << "Your birthdate is " << day << "." << month;
    return 0;
}