How to resolve the algorithm Loops/While step by step in the OCaml programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Loops/While step by step in the OCaml programming language

Table of Contents

Problem Statement

Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loops/While step by step in the OCaml programming language

Source code in the ocaml programming language

let n = ref 1024;;
while !n > 0 do
  Printf.printf "%d\n" !n;
  n := !n / 2
done;;

let rec loop n =
  if n > 0 then begin
    Printf.printf "%d\n" n;
    loop (n / 2)
  end
in loop 1024

  

You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the OCaml programming language
You may also check:How to resolve the algorithm MD5 step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Delegates step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the Elixir programming language
You may also check:How to resolve the algorithm Inheritance/Multiple step by step in the Eiffel programming language