Lesson 2
Summing Even Digits in an Integer with C++ Loops
Introduction

Welcome to another exciting session! In today's lesson, we face a unique coding challenge. We will traverse the digits of a number using a while loop under a specific condition. You will hone your skills in working with C++ loops and conditional statements, both of which are fundamental building blocks of programming. Shall we begin?

Task Statement

Our mission today is somewhat mind-twisting! We need to create a function that operates on an input integer to calculate the sum of its even-numbered digits. However, we won't convert this integer to a string at any point during this process. For example, given an integer n of value 4625, our output should be 12, which is the sum of the even digits 4, 6, and 2.

Remember, n will always be a positive integer that falls within the range from 11 to 10810^8. Are you ready for the challenge? Awesome! Let's get started!

Solution Building: Step 1

We begin by setting the basic structure for our function. In this step, we define a variable, digit_sum, that will accumulate the sum of the even digits.

Here's the initial framework of our function:

C
1int solution(int n) { 2 int digit_sum = 0; 3 // The function expands from here. 4}
Step 2: Setting up the Loop

The most effective tool for iterating through the digits of n is a while loop. The loop will run as long as n is greater than zero. Integrating this into our function produces:

C
1int solution(int n) { 2 int digit_sum = 0; 3 while (n > 0) { 4 // Further development of the function will proceed here. 5 } 6}
Step 3: Extracting and Processing Each Digit

Within our loop, we'll extract the last digit of n using the modulo operation (n % 10). If this digit is even, we add it to the digit_sum.

After we process a digit, we'll truncate the last digit of n using integer division (n / 10). This step readies the while loop for the next digit.

This is what the code looks like now:

C
1int solution(int n) { 2 int digit_sum = 0; 3 while (n > 0) { 4 int digit = n % 10; 5 if (digit % 2 == 0) { // Check if the digit is even. 6 digit_sum += digit; 7 } 8 n = n / 10; // Remove the last digit. 9 } 10}
Step 4: Returning the Result

Once we have summed all the even digits, the final step is to return our digit_sum.

Here is the completed function:

C
1int solution(int n) { 2 int digit_sum = 0; 3 while (n > 0) { 4 int digit = n % 10; 5 if (digit % 2 == 0) { // Check if the digit is even. 6 digit_sum += digit; 7 } 8 n = n / 10; // Remove the last digit. 9 } 10 return digit_sum; 11}
Lesson Summary

Congratulations on completing this lesson! You have navigated through foundational concepts, including using a while loop to iterate through the digits of a number and understanding how to apply conditions within that loop. Now, the baton is in your hands! Tackle more challenges to solidify and expand upon your newly acquired skills. Remember that your growth is limited only by the extent of your dedication. Keep practicing — your C++ competency is growing with each challenge you conquer!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.