Q:

Rust program to find the remainder without using the modulus (%) operator

belongs to collection: Rust Basic Programs

0

Here, we will read two integer numbers from the user and find the remainder without using the modulus (%) operator.

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 find the remainder without using the modulus (%) operator is given below. The given program is compiled and executed successfully.

// Rust program to find the remainder 
// without using '%' operator

use std::io;

fn main() {
    let mut n1:i32 = 0;
    let mut n2:i32 = 0;
    let mut rem:i32 = 0;
    
    let mut input1 = String::new();
    let mut input2 = String::new();
    
    println!("Enter number1: ");
    io::stdin().read_line(&mut input1).expect("Not a valid string");
    n1 = input1.trim().parse().expect("Not a valid number");

    println!("Enter number2: ");
    io::stdin().read_line(&mut input2).expect("Not a valid string");
    n2 = input2.trim().parse().expect("Not a valid number");

	rem=n1-(n1/n2)*n2;
 
	println!("Remainder is: {}",rem);    
}

Output:

RUN 1:
Enter number1: 
66
Enter number2: 
3
Remainder is: 0

RUN 2:
Enter number1: 
9
Enter number2: 
5
Remainder is: 4

Explanation:

Here, we read the value of n1n2 from the user. Then we found the remainder without using the modulus '%' operator. After that, we printed the result.

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

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to calculate the HCF (Highest Common ... >>
<< Rust program to subtract a number without using th...