diff options
added examples for sem 3
8 files changed, 204 insertions, 1 deletions
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | Sem. 3/Contains5.cpp | 32 | ||||
| -rw-r--r-- | Sem. 3/DevidersCount_PrimalityCheck.cpp | 29 | ||||
| -rw-r--r-- | Sem. 3/GCD_solution1.cpp | 32 | ||||
| -rw-r--r-- | Sem. 3/GCD_solution2.cpp | 35 | ||||
| -rw-r--r-- | Sem. 3/GCD_solution3_Eucid.cpp | 25 | ||||
| -rw-r--r-- | Sem. 3/PrintEvenNumbers.cpp | 27 | ||||
| -rw-r--r-- | Sem. 3/SumOfDigits.cpp | 22 |
@@ -2,4 +2,5 @@ - **Тема 1 (09.10.2020) :** Променливи. Типове променливи. Оператори. Преобразуване между типове. -.- **Тема 1 (16.10.2020) :** Приоритет на операторите. Асоциативност. Условни провеки (if, switch) + - **Тема 2 (16.10.2020) :** Приоритет на операторите. Асоциативност. Условни провеки (if, switch) + - **Тема 3 (23.10.2020) :** Жизнен цикъл на променливите. Оператори за цикъл(while, for). Оператор break. Задачи
\ No newline at end of file diff --git a/Sem. 3/Contains5.cpp b/Sem. 3/Contains5.cpp new file mode 100644 index 0000000..78d13f0 --- /dev/null +++ b/Sem. 3/Contains5.cpp @@ -0,0 +1,32 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + bool foundFive = false; + while (n != 0) + { + int lastDigit = n % 10; + if (lastDigit == 5) + { + foundFive = true; + break; + } + n /= 10; + } + + if (foundFive) + { + cout << "yes"; + } + else + { + cout << "no"; + } + + +}
\ No newline at end of file diff --git a/Sem. 3/DevidersCount_PrimalityCheck.cpp b/Sem. 3/DevidersCount_PrimalityCheck.cpp new file mode 100644 index 0000000..7584722 --- /dev/null +++ b/Sem. 3/DevidersCount_PrimalityCheck.cpp @@ -0,0 +1,29 @@ +#include <iostream> + +using namespace std; + +int main() +{ + unsigned int n; //allways positive int + cin >> n; + + int divCount = 0; + + for (int i = 1; i <= n; i++) + { + if (n%i == 0) + divCount++; + } + cout << divCount; + if (divCount == 2) + { + cout << "Prime"; + } + else + { + cout << "Not a prime"; + } + + + +}
\ No newline at end of file diff --git a/Sem. 3/GCD_solution1.cpp b/Sem. 3/GCD_solution1.cpp new file mode 100644 index 0000000..58ea35b --- /dev/null +++ b/Sem. 3/GCD_solution1.cpp @@ -0,0 +1,32 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n, k; + + cin >> n >> k; + + if (n < k) //swap + { + int temp = n; + n = k; + k = temp; + } + + int devider = 1; + int result; + while (devider <= k) + { + if (n % devider == 0 && k % devider == 0) + { + result = devider; + } + devider++; + } + + cout << result; + + +}
\ No newline at end of file diff --git a/Sem. 3/GCD_solution2.cpp b/Sem. 3/GCD_solution2.cpp new file mode 100644 index 0000000..ba06c89 --- /dev/null +++ b/Sem. 3/GCD_solution2.cpp @@ -0,0 +1,35 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n, k; + + cin >> n >> k; + + if (n < k) //swap + { + int temp = n; + n = k; + k = temp; + + } + int devider = k; + bool foundSolution = false; + + + while (!foundSolution) + { + if (n % devider == 0 && k % devider == 0) + foundSolution = true; + else + devider--; + + + } + cout << devider; + + + +}
\ No newline at end of file diff --git a/Sem. 3/GCD_solution3_Eucid.cpp b/Sem. 3/GCD_solution3_Eucid.cpp new file mode 100644 index 0000000..f190a90 --- /dev/null +++ b/Sem. 3/GCD_solution3_Eucid.cpp @@ -0,0 +1,25 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n, k; + + cin >> n >> k; + + if (n < k) //swap + { + int temp = n; + n = k; + k = temp; + } + + while (k != 0) + { + int div = n % k; + n = k; + k = div; + } + cout << n << endl; +}
\ No newline at end of file diff --git a/Sem. 3/PrintEvenNumbers.cpp b/Sem. 3/PrintEvenNumbers.cpp new file mode 100644 index 0000000..e9a2231 --- /dev/null +++ b/Sem. 3/PrintEvenNumbers.cpp @@ -0,0 +1,27 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + int j = 0; + while (j <= n) + { + cout << j << endl; + j += 2; + } + + //Here the variable j is reachable! + + + for (int i = 0; i <=n; i++) + { + cout << i << endl; + } + // Here the variable i is NOT reachable. + + +}
\ No newline at end of file diff --git a/Sem. 3/SumOfDigits.cpp b/Sem. 3/SumOfDigits.cpp new file mode 100644 index 0000000..6777a42 --- /dev/null +++ b/Sem. 3/SumOfDigits.cpp @@ -0,0 +1,22 @@ +#include <iostream> + +using namespace std; + +int main() +{ + int n; + cin >> n; + + int digitSum = 0; + + while (n != 0) + { + int lastDigit = n % 10; //extracts last digit + digitSum += lastDigit; //adds it to the sum + n /= 10; //removes last digit + } + + cout << digitSum; + + +}
\ No newline at end of file |
