Advertisement
Alekscho85

MagicDates

Oct 26th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System;
  2. namespace _16.MagicDates
  3. {
  4.     class MagicDates
  5.     {
  6.         static void Main()
  7.         {
  8.             Console.Write("Enter Start Year- ");
  9.             int startYear = int.Parse(Console.ReadLine());
  10.             Console.Write("Enter End Year- ");
  11.             int endYear = int.Parse(Console.ReadLine());
  12.             Console.Write("Enter Magic Weight- ");
  13.             int magicWeight = int.Parse(Console.ReadLine());
  14.  
  15.             DateTime startDate = new DateTime(startYear, 1, 1);
  16.             DateTime endDate = new DateTime(endYear, 12, 31);
  17.             int count = 0;
  18.             for (DateTime d = startDate; d <= endDate; d = d.AddDays(1))
  19.             {
  20.                 int d1 = d.Day / 10;
  21.                 int d2 = d.Day % 10;
  22.                 int d3 = d.Month / 10;
  23.                 int d4 = d.Month % 10;
  24.                 int d5 = (d.Year / 1000) % 10;
  25.                 int d6 = (d.Year / 100) % 10;
  26.                 int d7 = (d.Year / 10) % 10;
  27.                 int d8 = (d.Year / 1) % 10;
  28.                 int[] digits = { d1, d2, d3, d4, d5, d6, d7, d8 };
  29.                 int weight = 0;
  30.                 for (int first = 0; first < digits.Length; first++)
  31.                 {
  32.                     for (int second = first + 1; second < digits.Length; second++)
  33.                     {
  34.                         weight = weight + (digits[first] * digits[second]);
  35.                     }
  36.                 }
  37.                 //Console.WriteLine("{0:d2}-{1:d2}-{2:d2} weight={3}", d.Day, d.Month, d.Year, weight);
  38.                 if (weight == magicWeight)
  39.                 {
  40.                     Console.WriteLine("{0:d2}-{1:d2}-{2:d2}", d.Day, d.Month, d.Year);
  41.                     count++;
  42.                 }
  43.             }
  44.  
  45.             if (count == 0)
  46.             {
  47.                 Console.WriteLine("No");
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement