The source code to convert an integer number to binary using recursion is given below. The given program is compiled and executed successfully.
// Rust program to convert integer number
// to binary using recursion
fn dec2bin(num:i32)->i32
{
if num == 0
{
return 0;
}
else
{
return num % 2 + 10 * dec2bin(num / 2);
}
}
fn main() {
let num:i32=6;
let res = dec2bin(num);
println!("The binary equivalent is {}.",res);
}
Explanation:
In the above program, we created two functions dec2bin() and main(). The dec2bin() function is a recursive function, which is used to convert an integer number into binary and return the result to the calling function.
In the main() function, we called the dec2bin() function and printed the result.
Program/Source Code:
The source code to convert an integer number to binary using recursion is given below. The given program is compiled and executed successfully.
Explanation:
In the above program, we created two functions dec2bin() and main(). The dec2bin() function is a recursive function, which is used to convert an integer number into binary and return the result to the calling function.
In the main() function, we called the dec2bin() function and printed the result.
need an explanation for this answer? contact us directly to get an explanation for this answer