Answer to Question #272085 in C# for chi

Question #272085

Write a program that reverses the input number n. Formulate an equation to come up with the answer:

(Apply the three loop statements in your solutions) Sample input/output dialogue:


Enter a number: 1238 Input data

Reverse number: 8321 Output value


1st Solution using- for loop

2nd Solution using- while loop

3rd Solution using- do while loop


1
Expert's answer
2021-11-28T05:26:47-0500
class Program
{
    public static void Main()
    {


        Console.Write("Enter a number: ");
        string input = Console.ReadLine();


        string output = ReverseWithDoWhile(input);
        Console.WriteLine($"Reverse number: {output}");


        Console.ReadLine();
    }




    public static string ReverseWithFor(string text)
    {
        StringBuilder output = new StringBuilder(text.Length);


        for (int i = text.Length - 1; i >= 0; i--)
            output.Append(text[i]);


        return output.ToString(); ;
    }


    public static string ReverseWithWhile(string text)
    {
        StringBuilder output = new StringBuilder(text.Length);


        int i = text.Length - 1;
        while (i >= 0)
        {
            output.Append(text[i]);
            i--;
        }


        return output.ToString(); ;
    }


    public static string ReverseWithDoWhile(string text)
    {
        StringBuilder output = new StringBuilder(text.Length);


        int i = text.Length - 1;
        do
        {
            output.Append(text[i]);
        } while (--i >= 0);


        return output.ToString(); ;
    }
}

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