Advertisement
Klaxon

[C# Operators] Is Digit Seven

Jul 8th, 2013
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. // Write an expression that checks for given integer if its third digit (right-to-left) is 7. E. g. 1732 --> true.
  2.  
  3. using System;
  4.  
  5. class IsDigitSeven
  6. {
  7.     static void Main()
  8.     {
  9.         //Declaring needed variables
  10.         int number;
  11.         int numberToCheck;
  12.         int isNumberSeven;
  13.         bool isValid;
  14.  
  15.         // Making validation
  16.         do
  17.         {
  18.             Console.WriteLine("Please enter a number between 2147483647 and -2147483647: ");
  19.             isValid = int.TryParse(Console.ReadLine(), out number);
  20.         }
  21.         while (number < 2147483647 | number > 100 && isValid == false);
  22.  
  23.         // Making calculation to isolate the third digit of the number
  24.         numberToCheck = number / 100;
  25.         isNumberSeven = numberToCheck % 10;
  26.  
  27.         // If the number is 7..
  28.         if (isNumberSeven == 7)
  29.         {
  30.             // ..print on the console
  31.             Console.WriteLine("Bingo! The third digit of the number {0} is 7!", number);
  32.         }
  33.  
  34.         // If the number is NOT 7..
  35.         else
  36.         {
  37.             // ..print on the console
  38.             Console.WriteLine("Too bad. The third digit of the number {0} is NOT 7, it's {1}!", number, isNumberSeven);
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement