78 78 votes The following function computes $X^{Y}$ for positive integers $X$ and $Y$. int exp (int X, int Y) { int res =1, a = X, b = Y; while (b != 0) { if (b % 2 == 0) {a = a * a; b = b/2; } else {res = res * a; b = b - 1; } } return res; } Which one of the following conditions is TRUE before every iteration of the loop? $X^{Y} = a^{b}$ $(res * a)^{Y} = (res * X)^{b}$ $X^{Y} = res * a^{b}$ $X^{Y} = (res * a)^{b}$ Programming in C gatecse-2016-set2 programming loop-invariants normal + – Akash Kanase 23.7k views answer comment Share Follow Print See all 9 Comments 9 9 Comments reply Show 6 previous comments js__ commented Jan 11 reply Follow flag take X = 2, Y = 3 1 1 replyShare Siddiqui_Danish commented Feb 3 reply Follow flag I took the Opposite and got the same Answer 0 0 replyShare Khushraj_Singh commented Aug 22 reply Follow flag How to choose value of X & Y? Please anyone teach me 0 0 replyShare Please log in or register to add a comment.
1 1 vote by taking exmple we can solve it. Here i took X=4 & Y=2 Shivam Patidar answered Jul 15, 2018 Shivam Patidar comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Here's another good method.Take X:2 and Y:3 (I choose them because they are prime) X power Y is 8The program will have output as: But keep track of res, variable a and variable b as they are given in the option.Now the program will run as like this:Time 1 (after 1st iteration)res:2a:2b:2Time 2res:2a:4b:1Time 3res:8a:4b:0 On every time in each row the combination of res, a and b values is equal to X power Y for only option C. Tushar Rana answered Jan 11, 2025 Tushar Rana comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes take x=2,y=3 devagravatt answered 2 days ago devagravatt comment Share Follow 0 reply Please log in or register to add a comment.