Recursive summation of digits till the sum is less than 10

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DigitSum

{

    class DigitSum

    {

        static void Main(string[] args)

        {

            try

            {

                Console.Write("Enter date: ");

                string date = Console.ReadLine();

                int sum;

                do

                {

                    sum = 0;

                    for (int i = 0; i < date.Length; i++)

                    {

                        if (char.IsDigit(char.Parse(date.Substring(i, 1))))

                        {

                            sum = sum + int.Parse(date.Substring(i, 1));

                        }

                    }

                    date = sum.ToString();

                } while (sum >= 10);

                Console.WriteLine("Sum = " + sum);

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

            }

 

        }

    }

}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s