Computer Science, asked by bharathmohank7716, 7 months ago

Int g (int x, int n) {
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
x *= i + 1;
continue;
}
x--;
if (x == 0) {
break;
}
}
return x;
}

Answers

Answered by dsaha201171
0

Answer:

if you give x= 2 and n = 5 then x should return 10

Explanation:

Here first of all x = 2. Loop will start from 0. As 0%2==0 so it will go to the condition and new x will be x = 2 * 0+1 = 2. then i will be 1 and not execute the if condition and go to x--. Then the new value of x will 1. then comes i=2. in if condition the new value will be x = 1 * 2+1 = 3.. it will continue like this

i=0 -> x = 2 * 0+1 = 2

i=1 -> x-- = 1

i=2 -> x = 1 * 2+1 = 3

i=3 -> x-- = 2

i= 4 -> x = 2 * 4 + 1 = 10

i=5 here 5 !< 5 so for loop will stop working and final result is 10

so, x =  10

Similar questions