C# Program to generate a sum of digits

In this C# program, we will take input from the user and generate a sum of digits. For example, if the user input 88 then result is 16. 

C# Program to generate sum of digits Code:

private static void Main(string[] args)
        {
            Console.WriteLine("Enter a Number");
            int num, sum = 0, m, initialVal;
            num = int.Parse(Console.ReadLine());
            initialVal = num;
            while (num > 0)
            {
                m = num % 10;
                sum = sum + m;
                num = num / 10;
            }
            Console.WriteLine("Sum of digits of entered number {0} is {1}", initialVal, sum);
            Console.ReadLine();
        }

C# Program to generate sum of digits Output:

C#-Program-to-generate-sum-of-digits

 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice