Calculate Sum Of Digits of a Number in C#

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

  1. Start
  2. Declare the num, rem, sum, and x four integer variables.
  3. Initialize variable sum as zero (sum=0).
  4. Ask on the screen to enter an integer value and assign it to variable x.
  5. Check the x variable value, whether it is zero or not.
  6. If the x variable value is zero then return x.
  7. If the x variable value is not zero, then asinine x variable value to variable num (num=x).
  8. 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;  
    
  9. 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.
  10. Print the sum variable value on the screen.
  11. 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

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال