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;
        }
    }
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source