Write a Rust function to find the factorial of a given number using recursion.
Write a Rust function to find the factorial of a given number using recursion.
231
20-Jun-2023
Aryan Kumar
24-Jun-2023Sure, here is a Rust function to find the factorial of a given number using recursion:
Rust
This function first defines a base case. The base case is when the number is 0. In this case, the function returns 1. Otherwise, the function defines a recursive case. The recursive case is when the number is greater than 0. In this case, the function multiplies the number by the factorial of the number minus 1. Finally, the function returns the factorial.
To run this program, you can save it as a
.rsfile and then compile and run it using the following commands:Code snippet
This will prompt you to enter a number. Enter a number and press Enter. The program will then print the factorial of the number.
For example, if you enter the number 5, the program will print the following output:
Code snippet