Find the output of the following algorithm when an 6 bn 8

Find the output of the following algorithm when (a)n = 6 (b)n = 8 begin f : = 1; Input n; for i := 1 to n do f : = f * i; Output f; end

Solution

(a) n=6

f=1

fori=1, f=f*i = 1*1 = 1

fori=2, f = f*i = 1*2 = 2

for i=3, f = f*i = 2*3 = 6

for i=4, f = f*i = 6*4 = 24

for i=5, f = f*i = 24*5 = 120

for i=6, f = f*i = 120*6 = 720

final out put when n=6 is f=720

(b) n=8

f=1

fori=1, f=f*i = 1*1 = 1

fori=2, f = f*i = 1*2 = 2

for i=3, f = f*i = 2*3 = 6

for i=4, f = f*i = 6*4 = 24

for i=5, f = f*i = 24*5 = 120

for i=6, f = f*i = 120*6 = 720

for i=7, f = f*i = 720*7 = 5040

for i=8, f = f*i = 5040*8 = 40320

final out put when n=8 is f=40320

Find the output of the following algorithm when (a)n = 6 (b)n = 8 begin f : = 1; Input n; for i := 1 to n do f : = f * i; Output f; endSolution(a) n=6 f=1 fori=

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site