ADVERTISEMENT
ADVERTISEMENT

Rust program to demonstrate the recursion

Rust | Recursion Example: Write an example to demonstrate the recursion.
Submitted by Nidhi, on October 10, 2021

Problem Solution:

In this program, we will create a recursive function to print numbers from a given number to 1.

Program/Source Code:

The source code to demonstrate recursion is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate 
// the recursion

fn RecursiveFun(val:i32)->i32 {
	let mut num:i32=val;
	
	if num == 0 {
		return num;
	} else {
		print!("{} ", num);
	}
	num = num - 1;
	return RecursiveFun(num);
}

fn main() {
    RecursiveFun(10);
    println!();
}

Output:

10 9 8 7 6 5 4 3 2 1

Explanation:

In the above program, we created two functions RecursiveFun() and main(). The RecursiveFun() function is a recursive function, which is used to print numbers from a given number to 1.

In the main() function, we called RecursiveFun() 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.