Write a C# program to find the sum of digits

How to find the sum of digit in C#. In the below example another way to find the some of digit in C#.

Input sample:

123

Output:

6

Sum of digits program in C#

using System;

 

namespace CSharpProgram

{

    class Program

    {

        static void Main(string[] args)

        {

            //Define variables

            int num, sum = 0, m;

            Console.Write("Enter a number to find the sum of digits: ");

            num = int.Parse(Console.ReadLine());

            while (num > 0)

            {

                m = num % 10;

                sum = sum + m;

                num = num / 10;

            }

            Console.Write("Total sum= " + sum);

            Console.ReadLine();

        }

        

    }

}

More C# program with example

Find Fibonacci series in C# 

Check prime number in C#

 

Add a Comment

Your email address will not be published. Required fields are marked *

fourteen + twelve =