Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit917bf2164175f9436f0796cf433bd00fe42f8a5e (patch)
Author
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-10-17 13:59:42 +0300
Committer
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-10-17 13:59:42 +0300
Filesbd1c83994466171dccdb47b854d094a16263bf67
Introduction_to_programming_FMI-917bf2164175f9436f0796cf433bd00fe42f8a5e.tar
Introduction_to_programming_FMI-917bf2164175f9436f0796cf433bd00fe42f8a5e.tar.gz
Introduction_to_programming_FMI-917bf2164175f9436f0796cf433bd00fe42f8a5e.zip
Parent218309b4bc02dc94306faf60cd4bb4c73b1e675e (diff)
pract. 02 - Solutions
diff --git a/pract_SI/pract. 02/task1.cpp b/pract_SI/pract. 02/task1.cpp
new file mode 100644
index 0000000..3a60fad
--- /dev/null
+++ b/pract_SI/pract. 02/task1.cpp
@@ -0,0 +1,20 @@
+#include<iostream>
+using namespace std;
+int main()
+{
+ int year;
+
+ cout << "Enter an year to check: ";
+ cin >> year;
+
+ cout << year << " is";
+
+ if (!(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
+ {
+ cout << " not";
+ }
+
+ cout << " leap!\n";
+
+ return 0;
+} \ No newline at end of file
diff --git a/pract_SI/pract. 02/task2.cpp b/pract_SI/pract. 02/task2.cpp
new file mode 100644
index 0000000..a1ddcf8
--- /dev/null
+++ b/pract_SI/pract. 02/task2.cpp
@@ -0,0 +1,29 @@
+#include<iostream>
+using namespace std;
+int main()
+{
+ char inputCharacter;
+
+ cout << "Enter a character to test: ";
+ cin >> inputCharacter;
+
+ cout << inputCharacter << " is ";
+ if (inputCharacter == 'a' || inputCharacter == 'e' || inputCharacter == 'i' || inputCharacter == 'o' || inputCharacter == 'u' ||
+ inputCharacter == 'A' || inputCharacter == 'E' || inputCharacter == 'I' || inputCharacter == 'O' || inputCharacter == 'U')
+ {
+ cout << "a vowel!\n";
+ }
+ else
+ {
+ if ((inputCharacter >= 'a' && inputCharacter <= 'z') || (inputCharacter >= 'A' && inputCharacter <= 'Z'))
+ {
+ cout << "a consonant!\n";
+ }
+ else
+ {
+ cout << "is not a letter from the English alphabet!\n";
+ }
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/pract_SI/pract. 02/task3 - no EPSILON.cpp b/pract_SI/pract. 02/task3 - no EPSILON.cpp
new file mode 100644
index 0000000..6c9f832
--- /dev/null
+++ b/pract_SI/pract. 02/task3 - no EPSILON.cpp
@@ -0,0 +1,23 @@
+#include<iostream>
+#include<cmath>
+using namespace std;
+int main()
+{
+ double x, y;
+
+ cout << "Enter the first number: ";
+ cin >> x;
+
+ cout << "Enter the second number: ";
+ cin >> y;
+
+ if (fabs(x - y) < 0.000001)
+ {
+ cout << "Numbers must be different!\n";
+ return 1;
+ }
+
+ cout << (x < y ? y : x) << " is the greater number!\n";
+
+ return 0;
+} \ No newline at end of file
diff --git a/pract_SI/pract. 02/task3 - with EPSILON.cpp b/pract_SI/pract. 02/task3 - with EPSILON.cpp
new file mode 100644
index 0000000..dcd5f88
--- /dev/null
+++ b/pract_SI/pract. 02/task3 - with EPSILON.cpp
@@ -0,0 +1,24 @@
+#include <iostream>
+#include <cmath>
+using namespace std;
+
+int main()
+{
+ double num1, num2;
+
+ cout << "Enter the first number: ";
+ cin >> num1;
+
+ cout << "Enter the second number: ";
+ cin >> num2;
+
+ if (fabs(num1 - num2) <= DBL_EPSILON * fabs(num1))
+ {
+ cout << "Numbers must be different!\n";
+ return 1;
+ }
+
+ cout << (num1 > num2 ? "First" : "Second") << " is the greater number!\n";
+
+ return 0;
+} \ No newline at end of file
diff --git a/pract_SI/pract. 02/task4.cpp b/pract_SI/pract. 02/task4.cpp
new file mode 100644
index 0000000..acb471c
--- /dev/null
+++ b/pract_SI/pract. 02/task4.cpp
@@ -0,0 +1,26 @@
+#include<iostream>
+using namespace std;
+int main()
+{
+ double a, b, c;
+
+ cout << "First side: ";
+ cin >> a;
+
+ cout << "Second side: ";
+ cin >> b;
+
+ cout << "Third side: ";
+ cin >> c;
+
+ if ((a + b > c) && (a + c > b) && (b + c > a))
+ {
+ cout << "Triangle is valid.\n";
+ }
+ else
+ {
+ cout << "Triangle is not valid.\n";
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/pract_SI/pract. 02/task5.cpp b/pract_SI/pract. 02/task5.cpp
new file mode 100644
index 0000000..419d6d4
--- /dev/null
+++ b/pract_SI/pract. 02/task5.cpp
@@ -0,0 +1,51 @@
+#include<iostream>
+#include<cmath>
+using namespace std;
+int main()
+{
+ double a, b, c;
+ double root1, root2, imaginary;
+ double discriminant;
+
+ cout << "Finding the root of the equation a * x^2 + b * x + c = 0\n";
+
+ cout << "Enter the value of a: ";
+ cin >> a;
+
+ if (a < 0.00000001)
+ {
+ cout << "If a is 0, then this is not a quadratic equation!\n";
+ return 1;
+ }
+
+ cout << "Enter the value of b: ";
+ cin >> b;
+
+ cout << "Enter the value of c: ";
+ cin >> c;
+
+ discriminant = (b * b) - (4 * a * c);
+
+ if (discriminant > 0)
+ {
+ root1 = (-b + sqrt(discriminant)) / (2 * a);
+ root2 = (-b - sqrt(discriminant)) / (2 * a);
+
+ cout << "Two distinct and real roots exist: " << root1 << " and " << root2 << endl;
+ }
+ else if (discriminant == 0)
+ {
+ root1 = root2 = -b / (2 * a);
+
+ cout << "Two equal and real roots exist: " << root1 << " and " << root2 << endl;
+ }
+ else if (discriminant < 0)
+ {
+ root1 = root2 = -b / (2 * a);
+ imaginary = sqrt(-discriminant) / (2 * a);
+
+ cout << "Two distinct complex roots exist: " << root1 << "+" << imaginary << "i and " << root2 << "-" << imaginary << "i\n";
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/pract_SI/pract. 02/task6.cpp b/pract_SI/pract. 02/task6.cpp
new file mode 100644
index 0000000..a7484b2
--- /dev/null
+++ b/pract_SI/pract. 02/task6.cpp
@@ -0,0 +1,42 @@
+#include<iostream>
+using namespace std;
+int main()
+{
+ double a, b;
+ char operation;
+
+ cout << "Enter the first number: ";
+ cin >> a;
+
+ cout << "Enter the second number: ";
+ cin >> b;
+
+ cout << "Choose operation: +, -, / or *: ";
+ cin >> operation;
+
+ switch (operation)
+ {
+ case '+':
+ cout << "Result: " << a + b << endl;
+ break;
+ case '-':
+ cout << "Result: " << a - b << endl;
+ break;
+ case '*':
+ cout << "Result: " << a * b << endl;
+ break;
+ case '/':
+ if (b < 0.0000001)
+ {
+ cout << "You can not divide by zero!\n";
+ return 1;
+ }
+ cout << "Result: " << a / b << endl;
+ break;
+ default:
+ cout << "Invalid operation!\n";
+ break;
+ }
+
+ return 0;
+} \ No newline at end of file
diff --git a/pract_SI/pract. 02/task7.cpp b/pract_SI/pract. 02/task7.cpp
new file mode 100644
index 0000000..3c22a83
--- /dev/null
+++ b/pract_SI/pract. 02/task7.cpp
@@ -0,0 +1,44 @@
+#include<iostream>
+using namespace std;
+int main()
+{
+ // you can use int as well, but do you really need something as big as int?
+ short number;
+ unsigned char digit1, digit2, digit3, digit4, number1, number2;
+
+ cout << "Enter a 4-digit number: ";
+ cin >> number;
+
+ if (number < 1000 || number > 10000)
+ {
+ cout << "The input must be a 4-digit number!\n";
+ return 1;
+ }
+
+ digit1 = number / 1000;
+ digit2 = number / 100 % 10;
+ digit3 = number / 10 % 10;
+ digit4 = number % 10;
+
+ number1 = digit1 * 10 + digit4;
+ number2 = digit2 * 10 + digit3;
+
+ if (number1 < number2)
+ {
+ cout << "less (" << (int)number1 << " < " << (int)number2 << ")\n";
+ }
+ else
+ {
+ if (number1 == number2)
+ {
+ cout << "equal (" << (int)number1 << " = " << (int)number2 << ")\n";
+ }
+ else
+ {
+ cout << "greater (" << (int)number1 << " > " << (int)number2 << ")\n";
+ }
+ }
+
+
+ return 0;
+} \ No newline at end of file