BorislavBorisov

Task.01.02.Is Third Digit Seven

Oct 28th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. /* Напишете програма, която проверява дали на трета позиция, от ляво на дясно,
  3.  числото съдържа цифрата 7.Направете проверка дали числото е от 5 цифри.
  4.  Ако съдържа цифрата -> true, ако не я съдържа на трета позиция, но я има изпишете броя.
  5.  Ако не я съдържа (като цяло) също го отбележете.*/
  6. class IsThirdDigitSeven
  7. {
  8.     static void Main()
  9.     {
  10.         Console.WriteLine("Write a number of five digits:");
  11.         string number = Console.ReadLine();
  12.  
  13.         IsSevenDigitsNumber(number);
  14.     }
  15.  
  16.     static void IsSevenDigitsNumber(string number)
  17.     {
  18.         if(number.Length > 5 || number.Length < 5)
  19.         {
  20.             Console.WriteLine( "Write a number of 5 digits");
  21.             number = Console.ReadLine();
  22.             IsSevenDigitsNumber(number);
  23.         }
  24.         else if (number.Length == 5)
  25.         {
  26.             CheckContentSeven(number);
  27.         }
  28.     }
  29.  
  30.     static void CheckContentSeven(string checkDigit)
  31.     {
  32.         int counter = 0;
  33.         int counter2 = 0;
  34.         bool isThirdPos = false;
  35.         for (int i = 0; i < checkDigit.Length; i++)
  36.         {
  37.             if(checkDigit[i] == '7')
  38.             {
  39.                 counter++;
  40.                 if(i == 2)
  41.                 {
  42.                     isThirdPos = true;
  43.                     break;
  44.                 }
  45.             }
  46.             if (checkDigit[i] != '7')
  47.             {
  48.                 counter2++;
  49.             }
  50.            
  51.         }
  52.        
  53.         if (counter2 == checkDigit.Length)
  54.         {
  55.             Console.WriteLine("No contents the digit seven at all");
  56.         }
  57.         else if (isThirdPos)
  58.         {
  59.             Console.WriteLine("The number contents digit 7 at third position -> {0}", isThirdPos);
  60.         }
  61.         else if (!isThirdPos)
  62.         {
  63.             Console.WriteLine("The number contents 7, {0} times, but at the third position -> {1}", counter, isThirdPos);
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment