Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Exams / Exam1 / prob2.cpp
diff options
context:
space:
mode:
Commit0139d20b8844b4f1946c84b07e567faf5ba618c7 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-11-23 16:59:41 +0200
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-11-23 16:59:41 +0200
Filesd2d06058ccce1750c0a1cdc0ac590eac53487010 /Year 1/Programming introduction/Exams/Exam1/prob2.cpp
uni-projects-0139d20b8844b4f1946c84b07e567faf5ba618c7.tar
uni-projects-0139d20b8844b4f1946c84b07e567faf5ba618c7.tar.gz
uni-projects-0139d20b8844b4f1946c84b07e567faf5ba618c7.zip
Parent7a59e1811ccc5f2ff2d91647434a34900ca23f82 (diff)
Added my solutions to todays programming introduction exam
1 files changed, 47 insertions, 0 deletions
-rw-r--r--Year 1/Programming introduction/Exams/Exam1/prob2.cpp47
diff --git a/Year 1/Programming introduction/Exams/Exam1/prob2.cpp b/Year 1/Programming introduction/Exams/Exam1/prob2.cpp
new file mode 100644
index 0000000..c941112
--- /dev/null
+++ b/Year 1/Programming introduction/Exams/Exam1/prob2.cpp
@@ -0,0 +1,47 @@
+#include <iostream>
+
+using namespace std;
+
+bool isNeighbourNum(int n) {
+ while (n != 0) {
+ if (n % 10 == (n / 10) % 10)
+ return false;
+ n /= 10;
+ }
+ return true;
+}
+
+int subNeighbours(int a, int b) {
+ int smallest = 0, biggest = 0;
+
+ for (int i = a; i <= b; i++) {
+ if (isNeighbourNum(i)) {
+ if (i < smallest || !smallest)
+ smallest = i;
+ if (i > biggest || !biggest)
+ biggest = i;
+ }
+ }
+
+ if (!smallest || !biggest) {
+ return -1;
+ }
+
+ return biggest - smallest;
+}
+
+int main() {
+ cout << "a = ";
+ int a;
+ cin >> a;
+ cout << "b = ";
+ int b;
+ cin >> b;
+
+ int sub = subNeighbours(a, b);
+
+ if (sub == -1)
+ cout << "No adjacent numbers!" << endl;
+ else
+ cout << sub << endl;
+}