Task: Egyptian division
C: '---Ported from the c code example to BaCon by bigbass 

'==================================================================================
FUNCTION EGYPTIAN_DIVISION(long dividend, long divisor, long remainder) TYPE long 
'==================================================================================
'--- remainder is the third  parameter, pass 0 if you do not need the remainder
 
DECLARE powers[64] TYPE long 
DECLARE doublings[64] TYPE long 

	LOCAL i TYPE long
 
	FOR i = 0 TO  63 STEP 1  
		powers[i] = 1 << i
		doublings[i] = divisor << i
		IF (doublings[i] > dividend) THEN
			BREAK
		ENDIF
	NEXT

	LOCAL answer TYPE long
	LOCAL accumulator TYPE long
	answer = 0
	accumulator = 0
 
	WHILE i >= 0
		'--- If the current value of the accumulator added to the
		'--- doublings cell would be less than or equal to the
		'--- dividend then add it to the accumulator
		IF (accumulator + doublings[i] <= dividend) THEN
			accumulator = accumulator + doublings[i]
			answer = answer + powers[i]
		ENDIF
		DECR i
	WEND
 
	IF remainder THEN
		remainder = dividend - accumulator
		PRINT dividend ," / ", divisor, " = " , answer ," remainder " , remainder

        PRINT "Decoded the answer to a standard fraction"
        PRINT  (remainder + 0.0 )/ (divisor + 0.0) + answer
        PRINT

	ELSE 
		PRINT dividend ," / ", divisor , " = " , answer 
	ENDIF
	
	RETURN answer

ENDFUNCTION

 
	'--- the large number divided by the smaller number 
	'--- the third argument is 1 if you want to have a remainder
	'--- and 0 if you dont want to have a remainder 
	
	EGYPTIAN_DIVISION(580,34,1)
	EGYPTIAN_DIVISION(580,34,0)

EGYPTIAN_DIVISION(580,34,1)

C#: using System;
using System.Collections;

namespace Egyptian_division
{
	class Program
	{
		public static void Main(string[] args)
		{
			Console.Clear();
			Console.WriteLine();
			Console.WriteLine(" Egyptian division ");
			Console.WriteLine();
			Console.Write(" Enter value of dividend : ");
			int dividend = int.Parse(Console.ReadLine());
			     
			Console.Write(" Enter value of divisor : ");
			int divisor = int.Parse(Console.ReadLine());
			                         
			Divide(dividend, divisor);
			
			Console.WriteLine();
			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
			
			
			
		}
		
		static void Divide(int dividend, int divisor)
		{
			
			
			
			int result   = 0;
			int reminder = 0;
			
			int powers_of_two = 0;
			int doublings 	  = 0;
			
			int answer 	= 0;
			int accumulator = 0;
			
			int two = 2;
			int pow = 0;
			int row = 0;
			
			
			
			
			ArrayList table_powers_of_two = new ArrayList();
			ArrayList table_doublings     = new ArrayList();
			
			
			
			
			Console.WriteLine("                           ");
			Console.WriteLine(" powers_of_2     doublings ");
			Console.WriteLine("                           ");
			
			
			powers_of_two = 1;
			doublings = divisor;
			while( doublings <= dividend )
			{
				
				table_powers_of_two.Add( powers_of_two );
				table_doublings.Add( doublings );
				
				
				Console.WriteLine("{0,8}{1,16}",powers_of_two, doublings);
				
				
				pow++;
				
				powers_of_two = (int)Math.Pow( two, pow );
				doublings = powers_of_two * divisor;
			}
			Console.WriteLine("                           ");
			
			
			
			
			row = pow - 1;
			Console.WriteLine("                                                 ");
			Console.WriteLine(" powers_of_2     doublings   answer   accumulator");
			Console.WriteLine("                                                 ");
			Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop + row);
			
			pow--;
			while( pow >= 0 && accumulator < dividend )
			{
				
				doublings = int.Parse(table_doublings[pow].ToString());
				powers_of_two = int.Parse(table_powers_of_two[pow].ToString());
				
				if(accumulator + int.Parse(table_doublings[pow].ToString()) <= dividend )
				{
					
					accumulator += doublings;
					answer += powers_of_two;
					
					
					Console.ForegroundColor = ConsoleColor.Green;
					Console.Write("{0,8}{1,16}",powers_of_two, doublings);
					Console.ForegroundColor = ConsoleColor.Green;
					Console.WriteLine("{0,10}{1,12}", answer, accumulator);
					Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop - 2);
				}
				else
				{
					
					Console.ForegroundColor = ConsoleColor.DarkGray;
					Console.Write("{0,8}{1,16}",powers_of_two, doublings);
					Console.ForegroundColor = ConsoleColor.Gray;
					Console.WriteLine("{0,10}{1,12}", answer, accumulator);
					Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop - 2);
				}
				
				
				pow--;
			}
			
			Console.WriteLine();
			Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop + row + 2);
			Console.ResetColor();
			
			
			result = answer;
			if( accumulator < dividend )
			{
				reminder = dividend - accumulator;
				
				Console.WriteLine(" So " + dividend +
				                  " divided by " + divisor +
				                  " using the Egyptian method is \n " + result +
				                  " remainder (" + dividend + " - " + accumulator +
				                  ") or " + reminder);
				Console.WriteLine();
			}
			else
			{
				reminder = 0;
				
				Console.WriteLine(" So " + dividend +
				                  " divided by " + divisor +
				                  " using the Egyptian method is \n " + result +
				                  " remainder " + reminder);
				Console.WriteLine();
			}
		}
	}
}