w3resource

Rust Math Operations: Addition, Subtraction, Multiplication, Division

Rust Basic: Exercise-5 with Solution

Write a Rust program that performs basic Math operations - addition, subtraction, multiplication, and division operations on two integers.

Sample Solution:

Rust Code:

fn main() {
    // Declare two integer variables
    let num1 = 10;
    let num2 = 5;

    // Addition operation
    let addition_result = num1 + num2;
    println!("Addition result: {}", addition_result);

    // Subtraction operation
    let subtraction_result = num1 - num2;
    println!("Subtraction result: {}", subtraction_result);

    // Multiplication operation
    let multiplication_result = num1 * num2;
    println!("Multiplication result: {}", multiplication_result);

    // Division operation
    // Check if the divisor is not zero before performing division
    if num2 != 0 {
        let division_result = num1 / num2;
        println!("Division result: {}", division_result);
    } else {
        println!("Cannot divide by zero!");
    }
}

Output:

Addition result: 15
Subtraction result: 5
Multiplication result: 50
Division result: 2

Explanation:

Here's a brief explanation of the above Rust code:

  • fn main() { ... }: This is the entry point of the program, where execution begins.
  • let num1 = 10;: This line declares an integer variable named 'num1' and initializes it with the value 10.
  • let num2 = 5;: This line declares another integer variable named 'num2' and initializes it with the value 5.
  • let addition_result = num1 + num2;: This line adds the values of 'num1' and 'num2' and stores the result in the variable 'addition_result'.
  • println!("Addition result: {}", addition_result);: This line prints the result of the addition operation using the println! macro.
  • let subtraction_result = num1 - num2;: This line subtracts the value of 'num2' from 'num1' and stores the result in the variable 'subtraction_result'.
  • println!("Subtraction result: {}", subtraction_result);: This line prints the result of the subtraction operation using the println! macro.
  • let multiplication_result = num1 * num2;: This line multiplies the values of num1 and num2 and stores the result in the variable multiplication_result.
  • println!("Multiplication result: {}", multiplication_result);: This line prints the result of the multiplication operation using the println! macro.
  • if num2 != 0 { ... } else { ... }: This line checks if the value of 'num2' is not zero before performing the division operation. If 'num2' is zero, it prints a message indicating that division by zero is not allowed.
  • let division_result = num1 / num2;: This line divides the value of 'num1' by the value of 'num2' and stores the result in the variable 'division_result'.
  • println!("Division result: {}", division_result);: This line prints the result of the division operation using the println! macro.

Rust Code Editor:

Previous: Rust Program: Variable counter operations.
Next: Rust Program: Even or Odd number checker.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.