psotirov

Astrological Digits

Dec 10th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. using System;
  2.  
  3. class AstrologicalDigits
  4. {
  5.  
  6.     static int CalcAstroDigit(int number)
  7.     {
  8.         int result = 0; // initial result - empty
  9.         while (number > 0) // until empty number
  10.         {
  11.             result += number % 10; // extracts last digit
  12.             number /= 10; // shifts number one digit to the right
  13.         }
  14.         if (result > 9) result = CalcAstroDigit(result); // recursive call(s) until obtains the one digit result
  15.         return result;
  16.     }
  17.  
  18.     static void Main()
  19.     {
  20.         string input = Console.ReadLine(); // reads the number from the console
  21.         int number = 0; // initial sum
  22.         for (int i = 0; i < input.Length; i++)
  23.         {
  24.             int digit = input[i] - (int)'0'; // takes into account only digits
  25.             if (digit >= 0 && digit <= 9)
  26.             {
  27.                 number += digit;
  28.             }
  29.         }
  30.  
  31.         Console.WriteLine(CalcAstroDigit(number)); // prints the result on the console
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment