Welcome, guest | Sign In | My Account | Store | Cart

Using example: fibonacci.cmd 9 (returns 55).

Batch, 25 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@echo off
  setlocal
    ::condition for execution
    if "%1" leq "0" (
      echo The number must be greater than 'null'.
      goto:eof
    )
    ::variables
    set "a=0"
    set "b=1"
    set "c="
    set "i=0"
    ::cycle to retrieve Fibonacci number
    :loop
      if "%i%" equ "%1" goto:result
      set /a "c=%a% + %b%"
      set /a "a=%b%"
      set /a "b=%c%"
      set /a "i+=1"
      goto:loop
    ::print result
    :result
    echo %b%
  endlocal
exit /b
Created by greg zakharov on Wed, 14 Nov 2012 (MIT)
Batch recipes (42)
greg zakharov's recipes (59)

Required Modules

  • (none specified)

Other Information and Tasks