Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NOTE:PASS GOOGLE TEST CASES .NO NEED OF MAIN .USE RECURSION. WRITE A C++ RECURSIVE FUNCTION TO Print all numbers in range [100,999] such that sum

NOTE:PASS GOOGLE TEST CASES .NO NEED OF MAIN .USE RECURSION.

WRITE A C++ RECURSIVE FUNCTION TO Print all numbers in range [100,999] such that sum of digits at even position is equal to sum of digits at odd position

FUNCTION PROTOTYPE:

int* sum_of_digits(int start, int end, int size_of_resultant_array)

OUTPUT:

110 121 132 143 154 165 176 187 198 220 231 242 253 264 275 286 297 330 341 352 363 374 385 396 440 451 462 473 484 495 550 561 572 583 594 660 671 682 693 770 781 792 880 891 990

GOOGLE TEST CASES:

TEST(SumOFDigits, case1) { int* result = sum_of_digits(100, 999, 45); int* correct_answer = new int[45] {110, 121, 132, 143, 154, 165, 176, 187, 198, 220, 231, 242, 253, 264, 275, 286, 297, 330, 341, 352, 363, 374, 385, 396, 440, 451, 462, 473, 484, 495, 550, 561, 572, 583, 594, 660, 671, 682, 693, 770, 781, 792, 880, 891, 990}; for (int i = 0; i < 45; i++) { ASSERT_EQ(result[i], correct_answer[i]); }

CODE WITH LOOP

int sum_of_digits(int num, int start, int end) { int sum = 0; bool even = true; while (num > 0) { int digit = num % 10; if (even) { sum += digit; } else { sum -= digit; } num /= 10; even = !even; } return sum == 0; }

int main() {

const int start = 100; const int end = 999;

const int size_of_resultant_array = end - start + 1;

int arr[size_of_resultant_array];

int count = 0;

for (int i = start; i <= end; i++) {

if (sum_of_digits(i)) {

arr[count++] = i; } }

for (int i = 0; i < count; i++) {

std::cout << arr[i] << " "; }

std::cout << std::endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions