Tutorial Questions On Matlab
Tutorial Questions On Matlab
1. Write a MATLAB program using a while loop that computes the sum of positive
integers until it reaches the first sum greater than 100. The program should print the
computed sum and the number of integers summed.
Answer
total = 0;
num = 0;
while total <= 100
num = num + 1;
total = total + num;
end
disp(total)
disp(num)
2. Write a MATLAB program using a for loop that computes the sum of positive integers
from 1 to 100. The program should print the computed sum.
Answer
v = [1 : 100];
v_sum = 0;
for i = 1 : length(v)
v_sum = v_sum + v(i);
end
disp(v_sum)
3. Write a MATLAB program using a for loop that works through the string ‘Computer
Programming’ by printing the respective character in the string on a separate line.
Answer
a = 'Computer and Software Engineering';
for i = 1 : length(a)
disp(a(i))
end
4. Develop a MATLAB program that requests and accepts from a user two integers between
1 and 100. The program prints the result of the addition of the two integers when the user
provides the two integers in the correct range and prints ‘Incorrect Value’ when outside
range.
Answer
One = input("Enter a number between 1 and 100: ");
Two = input("Enter a number between 1 and 100: ");
if (One >= 1) && (One <= 100)
if (Two >= 1) && (Two <= 100)
disp(One + Two)
else
disp("Incorrect second value")
end
else
disp("Incorrect first value")
end
6. Develop a MATLAB program that computes and prints the volume of a cylinder, given
the radius and height of the cylinder to be 9m and 16m respectively.
Answer
radius = 9; height = 16;
volume = pi * radius^2 * height;
disp(volume)