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

 
We are nothing without our users ! You can help us offer even more high quality content. Please share our page !
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
1 + 4 =
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2022 CodingFusion
75 Important SQL Server queries you should know