Lesson 2
Traversing Digits and Summing Even Numbers in C#
Introduction

Welcome! In today's lesson, we'll delve into a unique coding challenge that centers around traversing the digits of a number using a while loop under a specific condition. You'll have the opportunity to practice and enhance your skills in working with C#'s loops and conditional statements — fundamental concepts in programming. Are you as excited as I am? Let's dive in!

Task Statement

Today, our objective is to create a method that operates on an integer input. The task might seem simple, but it requires some ingenuity. Here's the mission: given an integer, n, we need to calculate and return the sum of its even digits — and here's the clincher — without converting n into a string. For instance, if n equals 4625, the output should be 12 because the sum of the even digits 4, 6, and 2 equals 12.

Keep in mind that n will always be a positive integer between 1 and 100,000,000. Ready to give it a shot? Great! Let's get started!

Solution Building: Step 1

To start, we need the basic structure of our method, where we begin by defining a variable digitSum to keep track of the sum of even digits.

Below is the initial platform for our method:

C#
1public class Solution { 2 public int SumEvenDigits(int n) { 3 int digitSum = 0; 4 // Our code will evolve from here 5 return digitSum; 6 } 7}
Step 2: Setting up the Loop

The tool we've chosen to traverse through the digits of the input integer n is the while loop, which is set to run as long as n is greater than zero. Let's incorporate this into our method:

C#
1public class Solution { 2 public int SumEvenDigits(int n) { 3 int digitSum = 0; 4 while (n > 0) { 5 // We'll develop our method from here 6 } 7 return digitSum; 8 } 9}
Step 3: Extracting and Processing Each Digit

Inside our loop, we'll first extract the last digit of n using the modulo operation (n % 10). If the digit is even, we'll increase our digitSum by this digit.

After processing a digit, we'll then chop off the last digit of n using integer division (n / 10), which allows the while loop to proceed to the next digit. Here's how this appears in the code:

C#
1public class Solution { 2 public int SumEvenDigits(int n) { 3 int digitSum = 0; 4 while (n > 0) { 5 int digit = n % 10; 6 if (digit % 2 == 0) { // Check if the digit is even 7 digitSum += digit; 8 } 9 n = n / 10; // Remove the last digit 10 } 11 } 12}
Step 4: Returning the Result

After summing up all the even digits, the final step is to return our digitSum.

Our complete method now looks like this:

C#
1public class Solution { 2 public int SumEvenDigits(int n) { 3 int digitSum = 0; 4 while (n > 0) { 5 int digit = n % 10; 6 if (digit % 2 == 0) { // Check if the digit is even 7 digitSum += digit; 8 } 9 n = n / 10; // Remove the last digit 10 } 11 return digitSum; 12 } 13}
Adding a Main Method to Run the Code

To run our code and see it in action, we need to include a Main method. This method will create an instance of the Solution class, call SumEvenDigits with a test value, and print the result.

Here is the complete code with the Main method:

C#
1using System; 2 3public class Solution { 4 public int SumEvenDigits(int n) { 5 int digitSum = 0; 6 while (n > 0) { 7 int digit = n % 10; 8 if (digit % 2 == 0) { // Check if the digit is even 9 digitSum += digit; 10 } 11 n = n / 10; // Remove the last digit 12 } 13 return digitSum; 14 } 15 16 public static void Main(string[] args) { 17 Solution solution = new Solution(); 18 int result = solution.SumEvenDigits(4625); 19 Console.WriteLine("Sum of even digits: " + result); // Output should be "Sum of even digits: 12" 20 } 21}
Lesson Summary

Well done on completing this lesson! You've successfully navigated the foundational concepts of using a while loop to traverse the digits of a number and have gained an understanding of how to apply conditions within it. Now it's your turn to apply what you've learned. I invite you to explore additional challenges to solidify and build upon your new skill set. Remember, the only limit to your growth is the boundary of your dedication. Keep practicing; your C# prowess 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.