ADVERTISEMENT
ADVERTISEMENT

Rust program to calculate the factorial using recursion

Rust | Factorial using Recursion: Write a program to find the factorial of a given number using recursion.
Submitted by Nidhi, on October 10, 2021

Problem Solution:

In this program, we will create a recursive function to calculate the factorial of the given number and print the result.

Program/Source Code:

The source code to calculate factorial using recursion is given below. The given program is compiled and executed successfully.

// Rust program to calculate the 
// factorial using recursion

fn factorial(num:i32)->i32 {
	if num == 1 {
		return 1
	} else {
		return num * factorial(num-1)
	}
}

fn main() {
    let res = factorial(5);
    println!("Factorial is: {}",res);
}

Output:

Factorial is: 120

Explanation:

In the above program, we created two functions factorial() and main(). The factorial() function is a recursive function, which is used to calculate the factorial of the given number.

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

Rust Functions Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.