When working with numbers in programming, one common exercise is to calculate the sum of the digits of a number. This is a great way to get familiar with loops, conditionals, and working with integers in any language — including C#. In this blog post, we'll explore how to calculate the sum of digits of a number in C#, step-by-step.
Calculate Sum Of The Digits of a Number in C#
Getting Started
The sum of digits means adding all the individual digits of a number together. For example:
Input: 1234
Sum of digits = 1 + 2 + 3 + 4 = 10
This can be useful in tasks like:
- Validating data (e.g. checksum)
- Simple encryption techniques
- Coding challenges
Basic Approach Calculate Sum Of Digits
- Start
- Declare the num, rem, sum, and x four integer variables.
- Initialize variable sum as zero (sum=0).
- Ask on the screen to enter an integer value and assign it to variable x.
- Check the x variable value, whether it is zero or not.
- If the x variable value is zero then return x.
- If the x variable value is not zero, then asinine x variable value to variable num (num=x).
- Write the process to calculate the sum of digits using the below codes.
while(num>0) { rem=num%10; num=num/10; sum=sum+rem;
- the above code gets the rightmost digit of the number with help of remainder ‘%’ operator by dividing it with 10 and add it to sum. then divides the number by 10 with help of ‘/’ operator.
- Print the sum variable value on the screen.
- Exit.
Calculate Sum Of The Digits of a Number in C#
Program to Calculate Sum of Digits
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
int num, rem, sum, x;
sum = 0;
x = 1234;
Console.WriteLine("Original Number {0}",x);
if (x != 0)
{
num = x;
while (num > 0)
{
rem = num % 10;
num = num / 10;
sum = sum + rem;
}
}
else
{
sum = x;
}
Console.WriteLine($"\n sum of digits of {x} is {sum}");
}
}
Output
Original Number 1234
Sum of digits of 1234 is 10
Calculate Sum Of The Digits of a Number in C#
Calculate Sum of Digits Using String Conversion
Another approach is to convert the number to a string and loop through each character, This method is concise but slightly less efficient than the arithmetic method.
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
int sum, x;
string num;
sum = 0;
x = 1234;
Console.WriteLine("Original Number {0}",x);
num = x.ToString();
foreach (char c in num)
{
if (char.IsDigit(c))
{
sum += c - '0';
}
}
Console.WriteLine($"Sum of digits is: {sum}");
}
}
Output
Original Number 1234
Sum of digits is: 10
Handling Negative Numbers
If you're dealing with negative input, you can modify the code slightly to use the absolute value, This ensures the loop logic stays the same even if the number is negative.
int number = Math.Abs(int.Parse(Console.ReadLine()));
Summary
Calculating the sum of digits is a beginner-friendly problem that reinforces your understanding of loops, number manipulation, and conditionals. You can choose the approach that fits your use case—whether it's performance-focused arithmetic or easy-to-read string manipulation.
Thanks