Q:

Rust program to convert an integer number to binary using recursion

belongs to collection: Rust Functions Programs

0

In this program, we will create a recursive function to convert an integer number into binary and return the result to the calling function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

// 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to convert binary to Gray code using ... >>
<< Rust program to calculate the product of two numbe...