LardaX

MasterNumber(memory error)

Oct 2nd, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Master_Number
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int endNumber = int.Parse(Console.ReadLine());
  14.             for (int i = 1; i <= endNumber; i++)
  15.             {
  16.                 bool isPalindrome = IsPalindrome(i);
  17.                 bool isDivisibleBySeven = IsSumOfDigitsIsDivisibleBySeven(i);
  18.                 bool containsEvenDigit = ContainsEvenDigit(i);
  19.                 if (isPalindrome && isDivisibleBySeven && containsEvenDigit)
  20.                 {
  21.                     Console.WriteLine(i);
  22.                 }
  23.             }
  24.         }
  25.  
  26.         static bool IsPalindrome(int number)
  27.         {
  28.             string numberAsString = number.ToString();
  29.             string mirrorNumber = "";
  30.             for (int i = numberAsString.Length-1; i >= 0; i--)
  31.             {
  32.                 mirrorNumber += numberAsString[i];
  33.             }
  34.             if (mirrorNumber == numberAsString)
  35.             {
  36.                 return true;
  37.             }
  38.             return false;
  39.         }
  40.  
  41.         static bool IsSumOfDigitsIsDivisibleBySeven(int number)
  42.         {
  43.             int sumOfDigits = 0;
  44.             while (number != 0)
  45.             {
  46.                 sumOfDigits += number % 10;
  47.                 number /= 10;
  48.             }
  49.             if (sumOfDigits % 7 == 0)
  50.             {
  51.                 return true;
  52.             }
  53.             return false;
  54.         }
  55.  
  56.         static bool ContainsEvenDigit(int number)
  57.         {
  58.             while (number != 0)
  59.             {
  60.                 int lastDigit = number % 10;
  61.                 if (lastDigit % 2 == 0)
  62.                 {
  63.                     return true;
  64.                 }
  65.                 number /= 10;
  66.             }
  67.             return false;
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment