Consider the following algorithm assume N to be a positive i

Consider the following algorithm; assume N to be a positive integer.

          1. X ? 0

          2. Y ? 0

          3. WHILE (X < N)

                    a. X ? X+2

                    b. Y ? Y+X

          4. Y ? Y/N

What will this algorithm compute when N = 2? N = 5?

Solution

At N=2:

X=0

Y=0

While (0<2)

X=0+2=2

Y=0+2=2

... loop will execute one times

because after first time X=2 and (X<N)..(2<2)...which is false

so, X=2

and Y=Y/N=2/2=1

so, X=2 and Y=1.............Answer

At N=5:

X=0

Y=0

While (0<5)

X=0+2=2

Y=Y+X=0+2=2

since, X=2... which is <5 ... loop will again continue

While (2<5)

X=2+2=4

Y=2+4=6

since, X=4...which is less than 5..loop will again continue

while (4<5)

X=4+2=6

Y=6+6=12

now, X=6 which is greater than 5

so, loop will terminate

and we get

Y=12/5=2

so, X=6 and Y=2............Answer

Consider the following algorithm; assume N to be a positive integer. 1. X ? 0 2. Y ? 0 3. WHILE (X < N) a. X ? X+2 b. Y ? Y+X 4. Y ? Y/N What will this algor
Consider the following algorithm; assume N to be a positive integer. 1. X ? 0 2. Y ? 0 3. WHILE (X < N) a. X ? X+2 b. Y ? Y+X 4. Y ? Y/N What will this algor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site