C# Program To Find Sum of Digits of a Number - C# Program

C# Program To Find Sum of Digits of a Number

program to find sum of digits of a number. Example: if number = 3234 then sum of its digits = 3 + 2 + 3 + 4 = 12

using System;

namespace DigitSumApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, dsum = 0,r;
            
            Console.Write("Enter the Number");
            num = Convert.ToInt32(Console.ReadLine());
            int n = num;
            while(n!=0)
            {
                r = n % 10;
                dsum = dsum + r;
                n = n / 10;
            }
            Console.WriteLine("DigitSum of {0} is {1}", num, dsum);
        }
    }
}

Output - C# Program To Find Sum of Digits of a Number

Enter the Number :125
DigitSum of 125 is 8

C# Program To Find Sum of Digits of a Number