2 Lets say you wanted to calculate 13 Factorial 13 12 11


2. Let\'s say you wanted to calculate 13! (Factorial) (13 · 12 · 11 · 10 · 9 · 8 · 7 · 6 · 5 · 4 · 3 · 2 · 1).
*As we progress through the multiplication, we multiply the previous value by the new value and subtract 1 from the previous value.


Using a thought process similar to the factorial problem above, write a while loop that sums the
numbers 1 through 100.

define our starting value we will subtract 1 from n with each pass through the loop n 13 assign the starting value to another variable that will be the multiplication value while n >0 f f n; v% multiply previous value of f by new value of n n n-1; decrease the value of n for the next pass end f display result

Solution

1)

>> sum = double(0.0)

>>for n = 0:100

sum = sum + ( 1.0 / power(2,n) );

end

>> sum % sum always rounds to \"2\" because of internal rounding off , You need to increase the precision using digits()

2) I think the logic for your factorial program is wrong.

Before the start of the loop n = 13 and f =13. Now on the 1st iteration of the loop you multiply (13 *13), then multiply thsi value with 12 in second iteration and so on. In factorial case you should initialize f with \"1\" in the beginning.

Following is the code for the while loop to calculate the sum from 1 to 100

Kindly change the format of output using format command

>> format long

>> n = 100;

>> sum = 0;

while n > 0

sum = sum + n;

n = n - 1;

end

sum; % Your sum of 1 - 100

 2. Let\'s say you wanted to calculate 13! (Factorial) (13 · 12 · 11 · 10 · 9 · 8 · 7 · 6 · 5 · 4 · 3 · 2 · 1). *As we progress through the multiplication, we m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site