Q:

Rust program to multiply two numbers using \'+\' operator

belongs to collection: Rust Basic Programs

0

Here, we will read two numbers from the user. Then we will multiply both numbers using the '+' operator and print the result.

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 multiply two numbers using '+' operator is given below. The given program is compiled and executed successfully.

// Rust program to multiply two numbers 
// using '+' operator

use std::io;

fn main() {
    let mut n1:u32 = 0;
    let mut n2:u32 = 0;
    let mut cnt:u32 = 1;
    let mut mul:u32 = 0;
    let mut res:u32 = 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");

    
    while(cnt<=n2)
    {
        mul += n1;
        cnt = cnt+1;
    }
        
    println!("Multiplication is: {}",mul);
}

Output:

RUN 1:
Enter number1: 
10
Enter number2: 
20
Multiplication is: 200

RUN 2:
Enter number1: 
6
Enter number2: 
4
Multiplication is: 24

Explanation:

Here, we read the value of n1n2 from the user. Then we multiplied both numbers using the the '+' operator and 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 subtract a number without using th... >>
<< Rust program to demonstrate the use of abs() funct...