Skip to content
Snippets Groups Projects

Add Digits

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Kiryuu Sakuya
    Program.cs 640 B
    using System;
    using System.Collections.Generic;
    
    namespace leetcodecsharp
    {
        class Program
        {
            public static int AddDigits(int num)
            {
                if (num == 0)
                    return 0;
                int i = num;
                while (i.ToString().Length != 1)
                {
                    int j = 0;
                    foreach (char c in i.ToString())
                        j = j + (int)char.GetNumericValue(c);
                    i = j;
                }
                return i;
            }
            public static void Main(string[] args)
            {
                int a = 52515;
                Console.WriteLine(AddDigits(a));
            }
        }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment