Published on January 26, 2024By DeveloperBreeze

// Function to calculate the nth Fibonacci number iteratively
function fibonacci(n) {
    if (n <= 1) return n;

    let a = 0, b = 1;
    for (let i = 2; i <= n; i++) {
        let temp = a + b;
        a = b;
        b = temp;
    }

    return b;
}

// Calculate the 10th Fibonacci number
let nthFibonacci = fibonacci(10);

Comments

Please log in to leave a comment.

Continue Reading:

Calculate Factorial

Published on January 26, 2024

pythonjava

Generate Fibonacci Sequence

Published on January 26, 2024

python

Recursive Factorial Calculation in PHP

Published on January 26, 2024

php