ADVERTISEMENT
ADVERTISEMENT

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

Rust | Finding Division Example: Given two numbers, we have to find the division without using the division (/) operator.
Submitted by Nidhi, on October 07, 2021

Problem Solution:

In this program, we will calculate the division of two integer numbers without using the division (/) operator and print the result.

Program/Source Code:

The source code to calculate the division without using the '/' operator is given below. The given program is compiled and executed successfully.

// 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);
}

Output:

Result is: 12

Explanation:

In the above program, we created two functions division() and main(). The division() function is used to calculate the division of two integer numbers without using the '/' operator and return the result to the calling function.

In the main() function, we created three integer variables num1, num2, rs that are initialized with 48, 4, 0 respectively. Then we called the division() function and assigned the result to the rs variable. After that, we printed the result.

Rust Basic 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.