create a while loop that will multiply all even numbers between 2 & 10 in matlab

You can use a while loop and an if statement to multiply all even numbers between 2 and 10 in MATLAB. Below is an example code:

main.m
product = 1;
n = 2;

while n <= 10
    if rem(n, 2) == 0 % Check if n is even
        product = product * n; % Multiply even numbers only
    end
    n = n + 1;
end

fprintf('The product of even numbers between 2 and 10 is: %d\n', product);
241 chars
12 lines

In this code, we initialize the product variable to 1 and n variable to 2. We use a while loop to iterate through each number in the range of 2 to 10. We then use an if statement to check if the current value of n is even using the rem function (modulus). If n is even, we multiply it with the current value of product. We then increment n by 1 and continue with the loop. Once the loop is finished, we print out the final value of product.

The output of the above code will be:

main.m
The product of even numbers between 2 and 10 is: 384
53 chars
2 lines

which is correct as the product of even numbers between 2 and 10 is 2 × 4 × 6 × 8 × 10 = 384.

gistlibby LogSnag