Advertisement
sashomaga

Astrological Digits

Dec 24th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2.  
  3. //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:
  4. //(1)   Sums the digits of the number N and stores the result back in N.
  5. //(2)   If the obtained result is bigger than 9, step (1) is repeated, otherwise the algorithm finishes.
  6. //The last obtained value of N is the result, calculated by the algorithm.
  7. //Input
  8. //The input data should be read from the console.
  9. //The only line in the input contains a number N, which can be integer or real number (decimal fraction).
  10. //The input data will always be valid and in the format described. There is no need to check it explicitly.
  11. //Output
  12. //The output data should be printed on the console.
  13. //You must print the calculated astrological digit of the number N on the first and only line of the output.
  14.  
  15. class Program
  16. {
  17.     static void Main()
  18.     {
  19.         string num = Console.ReadLine();
  20.         int sum = new int();
  21.         foreach(char digit in num)
  22.         {
  23.             if ((digit != '.')&&(digit != '-'))
  24.             {
  25.                 sum += digit - '0';
  26.             }
  27.         }
  28.  
  29.         if (sum % 9 == 0 && sum != 0)
  30.         {
  31.             Console.WriteLine(9);
  32.         }
  33.         else
  34.         {
  35.             Console.WriteLine(sum % 9);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement