returns the maximum possible value obtained by inserting one '5' digit java code example

Example: program to find maximum number by inserting 5 in java

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Maximum possible number for {0} after inserting {1} is: {2}";

            Console.WriteLine(string.Format(str, 276, 3, MaximumPossible(276, 3)));
            Console.WriteLine(string.Format(str, -999, 4, MaximumPossible(-999, 4)));
            Console.WriteLine(string.Format(str, 0, 3, MaximumPossible(0, 3)));
            Console.WriteLine(string.Format(str, 860, 7, MaximumPossible(860, 7)));
            Console.ReadKey();
        }

        /// <summary>
        /// method to get the maximum possible number obtained by inserting given digit 
        /// at any position in the given number.
        /// </summary>
        /// <param name="num">given number</param>
        /// <param name="digit">digit to insert in given number</param>
        /// <returns>possible maximum number after inserting given digit at any position</returns>
        static int MaximumPossible(int num, int digit)
        {
            // edge case
            if (num == 0)
            {
                return digit * 10;
            }

            // -1 if num is negative number else 1
            int negative = num / Math.Abs(num);
            // get the absolute value of given number
            num = Math.Abs(num);
            int n = num;
            // maximum number obtained after inserting digit.
            int maxVal = int.MinValue;
            int counter = 0;
            int position = 1;

            // count the number of digits in the given number.
            while (n > 0)
            {
                counter++;
                n = n / 10;
            }

            // loop to place digit at every possible position in the number,
            // and check the obtained value.
            for (int i = 0; i <= counter; i++)
            {
                int newVal = ((num / position) * (position * 10)) + (digit * position) + (num % position);

                // if new value is greater the maxVal
                if (newVal * negative > maxVal)
                {
                    maxVal = newVal * negative;
                }

                position = position * 10;
            }

            return maxVal;
        }
    }
}