How to input i and get the left i digit of a variable in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 987262, i = 3;

            Console.WriteLine("The left {0} digit of the number {1} is: {2}", i, n, get_left_i_digit(n, i));
        }

        static int get_left_i_digit(int n, int i)
        {
            int k, len = n.ToString().Length;

            for (k = 0; k < len - i; k++)
                n /= 10;

            return n % 10;
        }
    }
}
/*
run:

The left 3 digit of the number 987262 is: 7

*/




answered Sep 19, 2014 by avibootz
edited Sep 19, 2014 by avibootz
...