Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Solutions Sem. 03 / 03. Remove_Middle_Digits.cpp
diff options
context:
space:
mode:
Commita69cd34091836c632c3268e646ac5a85fb9f8175 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-10-29 10:27:03 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-10-29 10:27:03 +0300
Files1f28671a0f73a702831bbbb68ccecbc734e79623 /Year 1/Programming introduction/Solutions Sem. 03/03. Remove_Middle_Digits.cpp
uni-projects-a69cd34091836c632c3268e646ac5a85fb9f8175.tar
uni-projects-a69cd34091836c632c3268e646ac5a85fb9f8175.tar.gz
uni-projects-a69cd34091836c632c3268e646ac5a85fb9f8175.zip
Parent2ec5faa88be1447cd93a301543f97ac9d6c312d7 (diff)
Added my solutions to Sem. 03 exercises
1 files changed, 25 insertions, 0 deletions
-rw-r--r--Year 1/Programming introduction/Solutions Sem. 03/03. Remove_Middle_Digits.cpp25
diff --git a/Year 1/Programming introduction/Solutions Sem. 03/03. Remove_Middle_Digits.cpp b/Year 1/Programming introduction/Solutions Sem. 03/03. Remove_Middle_Digits.cpp
new file mode 100644
index 0000000..c22a028
--- /dev/null
+++ b/Year 1/Programming introduction/Solutions Sem. 03/03. Remove_Middle_Digits.cpp
@@ -0,0 +1,25 @@
+#include <iostream>
+
+using namespace std;
+
+int main()
+{
+ int n;
+ cin >> n;
+
+ int digits = 0;
+ for (int i = n; i != 0; i /= 10)
+ digits++;
+
+ int digitsToTakeFromEachEnd = digits / 2 + (digits % 2 - 1);
+ int tenToTheHalf = 1; // ten to the power of digits to take from each end
+
+ for (int i = 0; i < digitsToTakeFromEachEnd; i++)
+ tenToTheHalf *= 10;
+
+ int tenToTheMiddle = (digits % 2 == 0) ? 100 : 10; // ten to the power of digits to remove from the middle
+
+ int resultNumber = (n % tenToTheHalf) + (n / tenToTheHalf / tenToTheMiddle) * tenToTheHalf;
+
+ cout << resultNumber << ", " << resultNumber + 1 << endl;
+}