Viacheslav

TelerikAlphaExamOctober11Problem01CrookedDigits

Oct 12th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TelerikExam11October
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             long sum = GettingIntegersFormString(Console.ReadLine());
  12.  
  13.             while (sum > 9)
  14.             {
  15.                 sum = SummingNumbers(sum);
  16.             }
  17.             Console.WriteLine(sum);
  18.         }
  19.         public static long GettingIntegersFormString (string input)
  20.         {
  21.             char[] chars = input.ToCharArray();
  22.             List<char> kistInput = new List<char>();
  23.             for (int i = 0; i < input.Length; i++)
  24.             {
  25.                 if (chars[i] != '.' && chars[i] != '-')
  26.                 {
  27.                     kistInput.Add(chars[i]);
  28.                 }
  29.             }
  30.             List<int> listOfInt = new List<int>();
  31.             for (int i = 0; i < kistInput.Count(); i++)
  32.             {
  33.                 int val =(int)Char.GetNumericValue(kistInput[i]);
  34.                 listOfInt.Add(val);
  35.             }
  36.             long sum = 0;
  37.             for (int i = 0; i < listOfInt.Count(); i++)
  38.             {
  39.                 sum += listOfInt[i];
  40.             }
  41.             return sum;
  42.         }
  43.         public static long SummingNumbers(long input)
  44.         {
  45.             string s = input.ToString();
  46.             char[] c = s.ToCharArray();
  47.             long sum = 0;
  48.             for (int i = 0; i < c.Length; i++)
  49.             {
  50.                 int val = (int)Char.GetNumericValue(c[i]);
  51.                 sum += val;
  52.             }
  53.             return sum;
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment