Write a method for division of two number without using division (‘/’) operator in C#.


    void DivideWithoutDivisionOperator(int divident,int divisior)
        {
            int quotient = 0;
            bool isNegative = false;
            if (divident < 0)//if negative
            {
                divident = -divident;//make positive
                if (divisior < 0)//if negative
                    divisior = -divisior;//make positive
                else
                    isNegative = true;
            }
            else if (divisior < 0)//if negative
            {
                divisior = -divisior;//make positive
                isNegative = true;
            }
            while(divident>=divisior)
            {
                divident =divident- divisior;
                quotient++;
            }
            if (isNegative == true)//if negative
                quotient = -quotient;//make result as negative
            Console.Write("Quotient"+quotient+" Reminder " + divident);
        }

Comments

Popular posts from this blog

Pagination of DataGrid in WPF using MVVM

Connect SQL Server Database to WPF Application and Perform CRUD Operations

Filter DataGrid and ListView in wpf using ICollectionView