In: Computer Science
Write two functions (must be titled “sumFW” and “sumFF” respectively) in MATLAB that does the following: take two integers x and y as lower limit and upper limit, then add and display the sum of all the integers between (including) the lower and upper limits. For sumFW you must use the while loop and for sumFF you must use the for loop
Here is the code:
sumFW(5, 9)
sumFF(5, 9)
sumFW(102, 200)
sumFF(102, 200)
function sumVal = sumFW(x, y)
%initialize with sum as 0 and val as x
sumVal = 0;
i = x;
%increase i till y is crossed
while i <= y
%update sum and increase i
sumVal = sumVal + i;
i = i + 1;
end
end
function sumVal = sumFF(x, y)
%start with sum as 0
sumVal = 0;
%for loop for x to y
for i=x:y
sumVal = sumVal + i;
end
end
Here is a screenshot of the code along with output of the
code:
Comment in case of any doubts.