EXERCISE 3 The counter in a for or while loop can be given e
EXERCISE 3 The counter in a for or while loop can be given explicit increment: for i -m:k:n to advance the counter i by k each time. In this problem we will evaluate the product of the first 10 odd numbers 19 in two ways: (a) Write a script file that evaluates the product of the first 10 odd numbers using a for loop. (b) Evaluate the product of the first 10 odd numbers using a single MATLAB command. Use the MATLAB command prod. while loop The while loop repeats a sequence of commands as long as some condition is met. The basic structure of a while loop is the following: while
Solution
Excercise - 3 Solution
a) Using a script file for multiplying first 10 odd integers
#include<stdio.h>
int main()
{
int Sum=1;
int i;
for(i=1;i<=19;i=i+2)
{
Sum = Sum * i;
}
printf(\"%d\ \", Sum);
return 0
}
b) In matlab you can use the inbulit product command by first defining the vector
A = [1:3:5:7:9:11:13:15:17:19]
B = prod(A)
B will contain the multiplication of all the numbers
