Advertisement
remote87

Find Nth Digit of a Number

Aug 26th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _04.NthDigit
  8. {
  9.     class NthDigit
  10.     {
  11.         static void Main()
  12.         {
  13.             Console.Write("Enter a number: ");
  14.             string number = Console.ReadLine();
  15.             Console.Write("Enter a position to check: ");
  16.             int position = int.Parse(Console.ReadLine());
  17.             if (position > 0 && position <= number.Length)
  18.             {
  19.                 int enteredNum = Convert.ToInt32(number);
  20.                 double nDigit = (enteredNum / Math.Pow(10, (position - 1))) % 10;
  21.                 Console.WriteLine((int)nDigit);
  22.             }
  23.             else
  24.             {
  25.                 Console.WriteLine("-");
  26.             }
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement