Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Write an expression that checks for given integer if its third
- * digit (left-to-right) is 7.
- */
- using System;
- class IsThirdDigit7FromLeft
- {
- static void Main()
- {
- long numberInput;
- do
- {
- Console.Write("Please enter integer number: ");
- }
- while (!long.TryParse(Console.ReadLine(), out numberInput));
- int numDigits = 0;
- long x = 1L; // help to count the digits
- long numberDivided = numberInput;// a number is used to count the digits
- while (x != 0)
- {
- x = numberDivided / 10;
- numberDivided = x;
- numDigits++;
- }
- int a = (int)(numberInput / Math.Pow(10, (numDigits - 3)));
- int b = (int) (a % 10);
- Console.WriteLine(a);
- Console.WriteLine(b);
- bool thirdDigit = (numberInput / Math.Pow(10, (numDigits - 3))) % 10 == 7; //(numDigits - 3) cos we need the third digit
- int value3rd = (int)(numberInput / Math.Pow(10, (numDigits - 3))) % 10; // finding what number is the third digit.
- Console.WriteLine("Is SEVEN (7) the third digit of the entered number {0}? {1}", numberInput, thirdDigit);
- if (!thirdDigit)
- {
- Console.WriteLine("The third digit (left-to-right) in the entered number {0} is {1}.", numberInput, value3rd);
- }
- else
- {
- // nothing happens
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement