Reverse integer in Asp c#

In this article I will explain you how to reverse an integer number programmatically and using inbuilt functions.



        <%--//==== Label to get input.--%>
        Before Reversed: 
        <%--//==== Label to show result--%>
        After Reversed: 


Code Behind


protected void Page_Load(object sender, EventArgs e)
        //Asp .Net C# Reverse Integer Number.
        int num = Convert.ToInt32(lblForInput.Text);
        //=== String variable to hold the result.
        string result = string.Empty;

        //==== We have to implement loop N times. N is number of digits in the number.
        //==== So lets first of all find how many digits are in number.
        int noOfCharacters = num.ToString().Length;

        //===== Now implement the loop logic.This loop will run equals to digits in the number.
        for (int i = 0; i < noOfCharacters; i++)
            //==== Now we will use % operator to divide the number.When divide using % operator it returns remainder.
            //==== We use 10 to divide the number as when we divide by 10 it always return the last digit as remainder.
            //====e.g. it returns last digit if number is not divisible by by 10 or return 0 if the number is divisible by 10.(In all cases we will get last digit).

            result += num % 10;

            //==== Now as we have got our last digit we have to remove last digit from number and assign it back to num.  "/" operator to divide.

            num = num / 10;


        //==== Print the result
        lblForResult.Text = result.ToString();

        //==== If you want to Convert result to integer.
        num = Convert.ToInt32(result);




Using Inbuilt functions:

 //======= Lets do by some tricy way using inbuilt c# string methods.
        num = Convert.ToInt32(lblForInput.Text);
        char[] charNum = num.ToString().ToCharArray().Reverse().ToArray();
        result = new string(charNum);

        //==== Print the result
        lblForResult.Text = result;


Final Output:

Reverse Integer in asp c#

We are nothing without our users ! You can help us offer even more high quality content. Please share our page !
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

8 + 3 =
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2017 CodingFusion