C# Program to find Sum of Digits

You are Here:

Find Sum of Digits

In the following example, we will add all the digits in the given number (12345) for just one time.

For example, 12345 = 1 + 2 + 3 + 4 + 5
i.e., 12345 = 15

Example

C# Compiler
using System; namespace myApp { class Program { static void Main(string[] args) { int num = 12; int copyNum = num; int total = 0; // Add Each digit from last digit while(copyNum != 0) { total += copyNum % 10; copyNum = copyNum / 10; } // result Console.Write("The sum of digit {0}: {1}", num, total); } } }

Find Sum of any given Digits

In the following example, we will find the sum of any given digits.

Example

C# Compiler
using System; namespace myApp { class Program { static void Main(string[] args) { Console.Write("Enter a (int) number: "); int num = Convert.ToInt32(Console.ReadLine()); int copyNum = num; int total = 0; // Add Each digit from last digit while(copyNum != 0) { total += copyNum % 10; copyNum = copyNum / 10; } // result Console.Write("The sum of digit {0}: {1}", num, total); } } }

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on C# with examples for quick and easy learning.

We are working to cover every Single Concept in C#.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author