Skip to main content
Skip to content

Functions · Intermediate

Fibonacci Series in Hindi Programming

Functions, recursive calls, returning values, and while loops.

The program

Edit anything and press Run. The output below was produced by running this exact code.

Expected output

Press Run to try it

--- Recursive Fibonacci ---Fibonacci( 0 ) = 0Fibonacci( 1 ) = 1Fibonacci( 2 ) = 1Fibonacci( 3 ) = 2Fibonacci( 4 ) = 3Fibonacci( 5 ) = 5Fibonacci( 6 ) = 8Fibonacci( 7 ) = 13

How it works

Each Fibonacci number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8 and so on. The function fibonacci(n) says exactly that in code — if n is 0 or 1 it returns n itself (the base case), otherwise it returns fibonacci(n - 1) + fibonacci(n - 2).

A function that calls itself is recursive. The agar (n <= 1) check is what stops the chain; without it the calls would never end and AltFLang would stop with a recursion-depth error.

The jab_tak loop at the bottom calls the function for 0 to 7 and prints each result. Recursion is elegant but slow for big numbers, because the same values are recalculated many times — a loop that keeps the last two numbers is the faster way.