Aspire's Library

A Place for Latest Exam wise Questions, Videos, Previous Year Papers,
Study Stuff for MCA Examinations - NIMCET

Previous Year Question (PYQs)



Consider the following pseudo-code fragment, where $a$ and $b$ are integer variables that have been initialized:

Pre-conditions: $(a>1\land a<b)$

Assume that overflow never occurs.

int $x=0$; int $p=1$;

while $(p<b)$ {
$p=p*a$;
$x=x+1$;
}

When the while loop terminates, what will be the value of $x$ in terms of $a$ and $b$?





Solution

Initially,

$p=1=a^0$

After $1$ iteration:

$p=a^1$

After $2$ iterations:

$p=a^2$

After $x$ iterations:

$p=a^x$

Loop stops when:

$p\ge b$

So,

$a^x\ge b$

Minimum such value of $x$ is:

$x=\lceil \log_a b\rceil$


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More

Ask Your Question or Put Your Review.

loading...