Answer to Question #307107 in C# for Kriss

Question #307107

Write a function solution that given an integer n and an integer k, returns the maximum possible three-digit value that can be obtained by performing at most K increases by 1 of any digit in N


1
Expert's answer
2022-03-07T16:45:27-0500
using System;


namespace Max3DigitValue
{
    internal class Program
    {
        // separating the high digit from other digits of number
        public static void DissectNumber(int number, out string high, out string low)
        {
            string num = number.ToString();
            high = num.Substring(0,1);
            low = num.Substring(1,num.Length-1);
        }


        static void Main(string[] args)
        {
            int n, k, h=0, number;
            string high, low, high_after_performing, result;
            bool InpErr;


            // input values of N and K with checking
            do
            {
                try
                {
                    Console.Clear();
                    Console.Write("Please input N value: ");
                    n = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
                    Console.Write("Please input K value: ");
                    k = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
                    InpErr = false;
                }


                catch
                {
                    InpErr = true; n = 0; k = 0;
                    Console.WriteLine("Wrong N or K value. Press any key to re-enter values...");
                    Console.ReadKey();
                }
            }
            while (InpErr);


            DissectNumber(n, out high, out low);
            high_after_performing = (h + Convert.ToInt32(high) + k).ToString(); // string representation of the high "part" of the number after the operation is performed
            number = Convert.ToInt32(high_after_performing +low); // string representation of the whole number after the operation is performed


            
            Console.WriteLine();


            if (number / 10 == 0) // if the number is single digit, add two leading zeros
            {
                result = "00" + number.ToString().Substring(number.ToString().Length - 1, 1);
                Console.WriteLine(result);
            }
            else
            if (number / 100 == 0) // if the number is double digit, add one leading zero
            {
                result = "0" + number.ToString().Substring(number.ToString().Length - 2, 2);
                Console.WriteLine(result);
            }
            else
            {
                result = number.ToString().Substring(number.ToString().Length - 3, 3);
                Console.WriteLine(result);
            }


        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS