Rust program to calculate the division without using division (/) operator

Everyone has every option I have Rust program to calculate the division without using division (/) operator I have the option to write this article. I am writing this article to resolve your doubts. We ask that you understand this article well.

Rust program to calculate the division without using division (/) operator

Rust program to calculate the division without using division (/) operator

// Rust program to calculate the 
// division without using '/' operator

fn division(mut num1:i32, mut num2:i32)->i32
{
   let mut negResult = false;
 
   if num1 < 0
   {
       num1 = -num1 ;
       if num2 < 0
       {
           num2 = - num2 ;
       }
       else
       {
           negResult = true;
       }
   }
   else if num2 < 0
   {
       num2 = - num2 ;
       negResult = true;
   }
 
   let mut quotient = 0;
   while num1 >= num2
   {
       num1 = num1 - num2 ;
       quotient=quotient+1 ;
   }
 
   if negResult == true
   {
        quotient = - quotient ;
   }
   return quotient ;
}

fn main() 
{
    let num1: i32  = 48;
    let num2: i32  = 4;
    let mut rs:i32 = division(num1,num2);

    println!("Division is: {}",rs);
}
Result is: 12

Final Words

We hope the Rust program to calculate the division without using division (/) operator article fills your doubts. And let us know if you have any doubts. We resolve your doubts Thank you.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment