diff options
Solved ex 4
1 files changed, 37 insertions, 1 deletions
| -rw-r--r-- | Year 1/Programming introduction/Homeworks/homework1.cpp | 38 |
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(); } |
