Q:

Rust program to print the Fibonacci series using recursion

belongs to collection: Rust Functions Programs

0

In this program, we will create a recursive function to print the Fibonacci series.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to print the Fibonacci series using recursion is given below. The given program is compiled and executed successfully.

// Rust program to print the 
// Fibonacci using recursion

fn printFibonacci(mut a:i32, mut b:i32, n:i32) {
	if n > 0 {
		let sum = a + b;
		print!("{} ", sum);
		a = b;
		b = sum;
		printFibonacci(a, b, n-1);
	}
}

fn main() {
    let mut a:i32 = 0;
    let mut b:i32 = 1;
    let n:i32 = 8;

    println!("Fibonacci series:");
    printFibonacci(a, b, n);
    println!();
}

Output:

Fibonacci series:
1 2 3 5 8 13 21 34

Explanation:

In the above program, we created two functions printFibonacci() and main(). The printFibonacci() function is a recursive function, which is used to print the Fibonacci series.

In the main() function, we called the printFibonacci() function and printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to calculate the power of a given num... >>
<< Rust program to calculate the factorial using recu...