Advertisement
sylviapsh

Astrological Digits

Dec 27th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. using System;
  2. class AstrologicalDigits
  3. {
  4.   static void Main()
  5.   {
  6.     //Telerik Academy
  7.     //The astrological digit of a given number N is a digit calculated by the number's digits by a special algorithm. The algorithm performs the following steps:
  8.     //(1)   Sums the digits of the number N and stores the result back in N.
  9.     //(2)   If the obtained result is bigger than 9, step (1) is repeated, otherwise the algorithm finishes.
  10.     //The last obtained value of N is the result, calculated by the algorithm.
  11.  
  12.     string numberInput = Console.ReadLine();
  13.     int sum = 0;
  14.     string buffer = numberInput;
  15.  
  16.     do
  17.     {
  18.       sum = 0;
  19.       foreach (char item in buffer)
  20.       {
  21.         if (item != '.' && item != '-')
  22.         {
  23.           sum += (int)item - '0';
  24.  
  25.         }
  26.       }
  27.       if (sum <= 9)
  28.       {
  29.         break;
  30.       }
  31.       else
  32.       {
  33.         buffer = Convert.ToString(sum);
  34.       }
  35.     } while (sum > 9);
  36.  
  37.     Console.WriteLine(sum);
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement