From 28d28bfb4c8fe765b93745af78ba9f06b334e65a Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 20 Nov 2021 21:30:24 +0200 Subject: Solved ex 4 --- .../Homeworks/homework1.cpp | 38 +++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'Year 1/Programming introduction/Homeworks/homework1.cpp') diff --git a/Year 1/Programming introduction/Homeworks/homework1.cpp b/Year 1/Programming introduction/Homeworks/homework1.cpp index f4ca9b9..c6832ad 100644 --- a/Year 1/Programming introduction/Homeworks/homework1.cpp +++ b/Year 1/Programming introduction/Homeworks/homework1.cpp @@ -69,6 +69,42 @@ void ex2() { cout << "Шипков мармалад: " << rosehipMarmalades << endl; } +/* + * Exercise 4 + */ + +bool isInteresting(int n) { + int lastDigit; + + do { + lastDigit = n % 10; + n /= 10; + + if ((lastDigit & 1) == ((n % 10) & 1)) + return false; + } while (n != 0); + + return true; +} + +int sumOfInterestingNumsBetween(int a, int b) { + int sum = 0; + + for (int i = a; i <= b; i++) { + if (isInteresting(i)) + sum += i; + } + + return sum; +} + +void ex4() { + int a, b; + cin >> a >> b; + + cout << sumOfInterestingNumsBetween(a, b) << endl; +} + int main() { - ex2(); + ex4(); } -- cgit v1.3.1-23-gd82d