You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
902 B
35 lines
902 B
use std::io;
|
|
use std::io::Write;
|
|
|
|
fn factorial(n: i64) -> i64 {
|
|
if n < 2 {
|
|
1
|
|
} else {
|
|
n * factorial(n - 1)
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
println!("****************************************************");
|
|
println!("Se ingresa un valor entero, se muestra su factorial.");
|
|
println!("Se utiliza un algoritmo recursivo.");
|
|
println!("****************************************************");
|
|
|
|
print!("n: ");
|
|
io::stdout().flush().expect("Error de escritura!");
|
|
|
|
let mut renglon: String = String::new();
|
|
io::stdin()
|
|
.read_line(&mut renglon)
|
|
.expect("Error de lectura!");
|
|
let n: i64 = renglon
|
|
.trim()
|
|
.parse::<i64>()
|
|
.expect("Se esperaba un numero entero!");
|
|
|
|
if n < 0 {
|
|
println!("ERROR: El algoritmo requiere un numero entero no negativo!");
|
|
} else {
|
|
println!("{}! es {}", n, factorial(n));
|
|
}
|
|
} |