Advertisement
geniusvil

operators and expressions - модификация задача 4

Nov 10th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. /*Write an expression that checks for given integer if its third
  2. * digit (left-to-right) is 7.
  3. */
  4.  
  5. using System;
  6.  
  7. class IsThirdDigit7FromLeft
  8. {
  9. static void Main()
  10. {
  11. long numberInput;
  12. do
  13. {
  14. Console.Write("Please enter integer number: ");
  15. }
  16. while (!long.TryParse(Console.ReadLine(), out numberInput));
  17.  
  18. int numDigits = 0;
  19. long x = 1L; // help to count the digits
  20. long numberDivided = numberInput;// a number is used to count the digits
  21. while (x != 0)
  22. {
  23.  
  24. x = numberDivided / 10;
  25. numberDivided = x;
  26. numDigits++;
  27.  
  28. }
  29.  
  30. int a = (int)(numberInput / Math.Pow(10, (numDigits - 3)));
  31. int b = (int) (a % 10);
  32. Console.WriteLine(a);
  33. Console.WriteLine(b);
  34. bool thirdDigit = (numberInput / Math.Pow(10, (numDigits - 3))) % 10 == 7; //(numDigits - 3) cos we need the third digit
  35. int value3rd = (int)(numberInput / Math.Pow(10, (numDigits - 3))) % 10; // finding what number is the third digit.
  36.  
  37. Console.WriteLine("Is SEVEN (7) the third digit of the entered number {0}? {1}", numberInput, thirdDigit);
  38. if (!thirdDigit)
  39. {
  40. Console.WriteLine("The third digit (left-to-right) in the entered number {0} is {1}.", numberInput, value3rd);
  41. }
  42. else
  43. {
  44. // nothing happens
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement