lmarkov

Third Digit Check

Nov 26th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System;
  2.  
  3. class ThirdDigitCheck
  4. {
  5.     static void Main()
  6.     {
  7.         int number, divisionBy100, checkForDidit7, digitsCount;
  8.  
  9.         Console.WriteLine("Enter number with minimum 3 digits:");
  10.  
  11.         if (int.TryParse(Console.ReadLine(), out number) && number >= int.MinValue && number <= int.MaxValue)
  12.         {
  13.             if (number < 0 && number.ToString().Length >= 4)
  14.             {
  15.                 divisionBy100 = (number / 100);
  16.                 checkForDidit7 = divisionBy100 % 10;
  17.                 Console.WriteLine();
  18.                 Console.WriteLine(checkForDidit7 == 7 ? "The third digit of your number {0} is 7" : "The third digit of your number {0} is not 7, it's {1}", number, checkForDidit7);
  19.                 Main();
  20.             }
  21.             else if (number > 0 && number.ToString().Length >= 3)
  22.             {
  23.                 divisionBy100 = (number / 100);
  24.                 checkForDidit7 = divisionBy100 % 10;                
  25.                 Console.WriteLine(checkForDidit7 == 7 ? "The third digit of your number {0} is 7\n" : "The third digit of your number {0} is not 7, it's {1}\n", number, checkForDidit7);
  26.                 Main();
  27.             }
  28.             else
  29.             {
  30.                 if(number < 0)
  31.                 {
  32.                     digitsCount = number.ToString().Length-1;
  33.                 }
  34.                 else
  35.                 {
  36.                     digitsCount = number.ToString().Length;
  37.                 }
  38.  
  39.                 Console.WriteLine(digitsCount == 1 ? "Your number contains only {0} digit!\n" : "Your number contains only {0} digits!\n", digitsCount);
  40.                 Main();
  41.             }
  42.            
  43.         }
  44.         else
  45.         {
  46.             Console.WriteLine("Invalid input!\n");                
  47.             Main();
  48.         }
  49.        
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment