Today i have attened an IT Services Company interview, where i was been asked to write the code for Reverse an Integer Value in C# with out using any Built In functions.

Here is the code i formulated in my mind. Go through it.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp02
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Write("Enter the Integer to Reverse: ");

            //Reading the Entered Number
            string  strInVal = Console.ReadLine();


            int inVal = Convert.ToInt32(strInVal);

            //Calling Reverse Function
            int outVal = Reverse(inVal);

            Console.WriteLine(String.Format("Revered Interger value is {0}", outVal));

            Console.WriteLine("**** Press Any Key to Exit ");

            Console.Read();


        }


        public static int Reverse(int inVal)
        {
            int result = 0;


            //Ex: 567
            // Loop 1:
            // 567 % 10 = 7
            // (0 * 10) + 7 = 7
            // inVal = 567 /10 = 56
            // Loop 2:
            // 56 % 10 = 6
            // (7 * 10) + 6  = 76
            // 56 / 10 =  5
            // Loop 3: 
            // 5 % 10 = 5
            // (56 * 10) + 5  = 765  -- Achieved out result. 
            // 5 / 10 = 0 // Do while condition ends
            do
            {
              

                result = (result * 10) + (inVal % 10);

                inVal = inVal / 10;
            }
            while (inVal > 0);

            return result;

        }
    }
}

Results
reverse result 1
reverse result 2

Kudos!!! I hope it will defenitly be useful to some body. Unfortunately i was n’t been able to write it out in my paper in interview time, just after returning the paper only i got this idea, but no use, i thought asking paper back and write n give. Anyway’s some day it will be useful for some one.. KOOOOLLL