The source code to subtract a number without using the '-' operator is given below. The given program is compiled and executed successfully.
// Rust program to subtract a number
// without using '-' operator
use std::io;
fn main()
{
let mut n1:i32 = 0;
let mut n2:i32 = 0;
let mut sub: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");
sub = n1 + (!n2) + 1;
println!("Subtraction is: {}",sub);
}
Output:
RUN 1:
Enter number1:
10
Enter number2:
20
Subtraction is: -10
RUN 2:
Enter number1:
76
Enter number2:
53
Subtraction is: 23
Explanation:
Here, we read the value of n1, n2 from the user. Then we subtracted n2 from n1 using the '!' operator and printed the result.
Program/Source Code:
The source code to subtract a number without using the '-' operator is given below. The given program is compiled and executed successfully.
Output:
Explanation:
Here, we read the value of n1, n2 from the user. Then we subtracted n2 from n1 using the '!' operator and printed the result.
need an explanation for this answer? contact us directly to get an explanation for this answer