Initializing help system before first use

Largest common divisor


Type: Programming
Rating: 2 (easy-medium)
Description: Find the largest common divisor of two integer numbers
  • recursive function calls
  • if-then-elif-then-else statement
  • alternative formulation using a 'while' loop
File(s): lcdiv.mos


lcdiv.mos
(!*******************************************************
  * Mosel Example Problems                              *
  * ======================                              *
  *                                                     *
  * file lcdiv.mos                                      *
  * ```````'``````                                      *
  * Example for the use of the Mosel language           *
  * (Largest common divisor of two numbers)             *
  *                                                     *
  * (c) 2008 Fair Isaac Corporation                     *
  *     author: S. Heipcke, 2001                        *
  *******************************************************!)

model Lcdiv                    ! Start a new model

function lcdiv(A,B:integer):integer
 if(A=B) then
  returned:=A
 elif(A>B) then
  returned:=lcdiv(B,A-B)
 else
  returned:=lcdiv(A,B-A)
 end-if   
end-function

declarations
 A,B: integer                  ! Two integer numbers
end-declarations

 write("Enter two integer numbers:\n  A: ")
 fflush
 readln(A)
 write("  B: ")
 fflush
 readln(B)

 writeln("Largest common divisor: ", lcdiv(A,B))

(!
 Instead of using a recursive function call, it is also possible to 
 formulate this program as a while loop:
  
 while (A <> B) do
  if (A>B) then 
   A:=A-B 
  else B:=B-A 
  end-if
 end-do
 
 writeln("Largest common divisor: ", A) 
!)

end-model