Reminder: The source code of the C++ programming language is organized into functions.
#include<iostream>
using namespace std;
int main() {
;
variable declaration;
program statements}
#include <iostream>
using namespace std;
// main function
int main() {
/*
print messages to the screen
*/
<< "Welcome to FINKI!" << endl;
cout return 0;
}
#include
- directive to include external
libraries.<iostream>
- library for working with standard
input/output streams (keyboard, screen).cout
- global object for working with the output stream
(screen).<<
- operator for printing to the output stream
(screen).<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;
<< "Enter a value for the variable value: ";
cout >> value;
cin << "The entered value is: " << value << '\n';
cout }
In the C++ programming language, variables are declared with the following syntax:
data_type variable_name = initial_value;
data_type
refers to the type of data that the variable
will store, such as an integer, a floating-point number, a character,
etc.variable_name
denotes the name of the variable, which
should be unique within a given code block. Variable names can be a
combination of letters, digits, and the underscore character
(_
), but they must start with a letter or an underscore.
Variable names should clearly indicate what is stored in them.Examples of variable declarations of different types:
int number = 5;
float price = 7.99;
char letter = 'a';
bool is_true = true;
Integer | Character | Floating-Point | Boolean |
---|---|---|---|
int | char | float | bool |
short | double | ||
long | long double |
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
*/
Operators are applied to numbers (integers or decimals):
Operator | Operation |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder when dividing) |
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);
<< "x = " << x << endl;
cout return 0;
}
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;
<< "The square of " << x << " is " << x * x << endl;
cout return 0;
}
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);
<< "The perimeter is: " << perimeter << endl;
cout << "The area square is: " << area << endl;
cout return 0;
}
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
;
<< "The arithmetic mean is: " << average << endl;
cout return 0;
}
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;
<< "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;
cout return 0;
}
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;
>> radius;
cin
float perimeter = 2 * radius * 3.14;
float area = radius * radius * 3.14;
<< "Perimeter = " << perimeter << endl;
cout << "Area = " << area << endl;
cout return 0;
}
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;
>> x >> y;
cin
<< x << " + " << y << " = " << x + y << endl;
cout << x << " - " << y << " = " << x - y << endl;
cout << x << " * " << y << " = " << x * y << endl;
cout << x << " % " << y << " = " << x % y << endl;
cout return 0;
}
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;
<< "Enter an uppercase letter: " << endl;
cout >> uppercase;
cin << uppercase << " is written in lowercase as " << char(uppercase + ('a' - 'A')) << endl;
cout return 0;
}
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;
<< "Enter the product price: ";
cout >> price;
cin << "The total price of the product is " << price * 1.18;
cout return 0;
}
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;
<< "Enter the product price: " << endl;
cout >> price;
cin << "Enter the number of installments: ";
cout >> installments;
cin << "Enter the interest rate: ";
cout >> interestRate;
cin = price * (1 + interestRate / 100);
total = total / installments;
installment
cout
<< "One installment will be: " << installment << endl;
<< "The total amount paid will be: " << total;
cout return 0;
}
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;
<< "Enter the number: ";
cout >> number;
cin << "The most significant digit is " << (number / 100);
cout << ", and the least significant digit is " << (number % 10);
cout return 0;
}
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;
<< "Enter your birthdate: ";
cout >> date;
cin = date / 1000000;
day = (date / 10000) % 100;
month << "Your birthdate is " << day << "." << month;
cout return 0;
}