---
title: "Implement a function to find the factorial of a given number iteratively in Rust."  
description: "Implement a function to find the factorial of a given number iteratively in Rust."  
author: "Utpal Vishwas"  
published: 2023-06-22  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158834/implement-a-function-to-find-the-factorial-of-a-given-number-iteratively-in-rust  
category: "rust"  
tags: ["programs", "rust"]  
reading_time: 2 minutes  

---

# Implement a function to find the factorial of a given number iteratively in Rust.

Implement a [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to find the factorial of a given number iteratively in Rust.

## Replies

### Reply by Aryan Kumar

Sure, here is a Rust function to find the factorial of a given number iteratively in Rust:

Rust

```plaintext
fn factorial(n: i32) -> i32 {
    let mut factorial = 1;
    for i in 1..=n {
        factorial *= i;
    }
    return factorial;
}

fn main() {
    let n = 5;
    let factorial = factorial(n);
    println!("The factorial of {} is {}", n, factorial);
}
```

This function first defines a function called `factorial` that takes an i32 number as input and returns the factorial of the number. The function uses an iterative approach to calculate the factorial. The function starts with a factorial of 1. Then, it iterates through the numbers from 1 to n, multiplying the factorial by each number. The final value of the factorial is returned.

The main function of the program then calls the `factorial` function with the value 5 as input. The result of the function call is then printed to the console.

To run the program, you can save it as a file called `factorial.rs` and then compile it using the following command:

Code snippet

```plaintext
rustc factorial.rs
```

Once the program is compiled, you can run it using the following command:

Code snippet

```plaintext
./factorial
```

This will print the following output to the console:

Code snippet

```plaintext
The factorial of 5 is 120
```


---

Original Source: https://www.mindstick.com/forum/158834/implement-a-function-to-find-the-factorial-of-a-given-number-iteratively-in-rust

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
