Lesson 2
Traversing and Summing Even Digits in Go
Introduction

Welcome to another exciting session! In today's lesson, we delve into a unique coding challenge using Go. We will traverse the digits of a number using a for loop under specific conditions. This exercise will help you sharpen your skills in working with Go loops and conditional statements, both of which are fundamental building blocks of programming in Go. Are you ready to get started?

Task Statement

Our mission today is intriguing! We need to create a function that calculates the sum of even-numbered digits from a given integer. We'll avoid converting the integer to a string at any point. For example, given the number 4625, our function should return 12, which is the sum of the even digits 4, 6, and 2.

The integer n will always be positive and within the range of 1 to 10810^8. Are you prepared for this challenge? Fantastic! Let's begin our journey into coding with Go!

Solution Building: Step 1

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

Here's how we begin:

Go
1func solution(n int) int { 2 var digitSum int 3 // The function expands from here. 4}
Step 2: Setting up the Loop

Recall that in Go, we can mimic a while loop using a for loop by specifying only the condition. We use this approach to iterate through the digits of the integer n as long as n is greater than zero. This allows us to process each digit from the least significant to the most significant.

Integrating this into our function, we have:

Go
1func solution(n int) int { 2 var digitSum int 3 for n > 0 { // Equivalent to a while loop 4 // Further development of the function will proceed here. 5 } 6}

This for loop iterates until the condition n > 0 is no longer satisfied, effectively serving the same purpose as a while loop in other languages.

Step 3: Extracting and Processing Each Digit

Inside our loop, we'll extract the last digit of n using the modulo operation (n % 10). If this digit is even, we'll add it to digitSum. After processing this digit, we truncate it from n using integer division (n / 10). This prepares the loop for the next digit.

Here's what our code looks like now:

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

Once all even digits have been summed, the final step is to return digitSum.

Below is the completed function:

Go
1func solution(n int) int { 2 var digitSum int 3 for n > 0 { 4 digit := n % 10 5 if digit % 2 == 0 { // Check if the digit is even. 6 digitSum += digit 7 } 8 n = n / 10 // Remove the last digit. 9 } 10 return digitSum 11}
Lesson Summary

Congratulations on successfully completing this lesson! You've explored foundational concepts in Go, utilizing a for loop to traverse the digits of a number and applying conditions to process those digits. Now it's your turn to individually tackle more challenges to solidify and expand upon these programming skills. Remember, practice is key to mastering Go. Stay dedicated, and you'll find your proficiency in Go growing with each coding task you tackle!

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