Function Sumdigits - C# Programming Exercise

In this exercise of C#, you will learn how to create a function named SumDigits that receives a number and returns the result of summing its digits. This type of exercise is very useful for practicing how to work with numbers and perform mathematical operations in C#. The function takes a number, breaks it down into its digits, and sums them. In the example, if the number is 123, the function will return 6, which is the sum of 1 + 2 + 3. This exercise will help you understand how to manipulate the digits of a number and apply simple operations in C#.

Learn to work with numbers and their digits with this hands-on function in C# and enhance your programming skills by performing basic mathematical operations.

 Category

Functions

 Exercise

Function Sumdigits

 Objective

Write a C# function SumDigits that receives a number and returns any results in the sum of its digits. For example, if the number is 123, the sum would be 6.

Console.Write( SumDigits(123) );
6

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Main method where the program starts
    public static void Main()
    {
        // Calling the SumDigits function with the number 123 and printing the result
        Console.WriteLine(SumDigits(123)); // The sum of digits of 123 is 6, which will be printed
    }

    // Function to calculate the sum of digits of a given number
    public static int SumDigits(int number)
    {
        // Variable to store the sum of the digits
        int sum = 0;

        // While loop to iterate through each digit of the number
        while (number > 0)
        {
            // Add the last digit of the number to sum
            sum += number % 10; // The modulus operation gets the last digit of the number
            number /= 10; // Removing the last digit by dividing the number by 10
        }

        // Returning the sum of digits
        return sum; // The result is the sum of all digits
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function Factorial

    In this exercise of C#, you will learn how to create a recursive function to calculate the factorial of a number. The factorial of a number is ex...

  •  Function Parameters of Main, Reverse

    In this exercise of C#, you will learn how to create a program named reverse that receives several words from the command line and display...

  •  Function GetInt

    In this exercise of C#, you will learn how to create a function named GetInt, which displays on screen the text received as a parameter, asks the user f...

  •  Function tasks database

    In this exercise of C#, you will improve a "tasks database" program by splitting it into multiple functions. This type of exercise is great for l...

  •  Function Greatest value in a array

    In this exercise of C#, you will learn how to create a function that returns the greatest value stored in an array of real numbers, which is specified as a par...

  •  Function factorial (iterative)

    In this exercise of C#, you will learn how to create an iterative (non-recursive) function to calculate the factorial of the number specified as a parameter. T...