Consider the following Pascal function:
Function X(M:integer):integer;
Var i:integer;
Begin
i := 0;
while i*i < M
do i:= i+1
X := i
end
The function call , if is positive, will return
View worked solution
Answer: C
To find what the function returns for a positive integer , let us analyze the loop condition and termination behavior.
The loop starts with and increments by in each iteration as long as:
The loop terminates as soon as this condition becomes false, which means it stops at the smallest non-negative integer such that:
Taking the square root on both sides (since and ):
The smallest integer that is greater than or equal to is, by definition, the ceiling of :
Dry Run Examples:
- Case 1: (a perfect square)
- (True)
- (True)
- (True)
- (False) Loop terminates.
- Returns , which is .
- Case 2: (not a perfect square)
- (True)
- (True)
- (True)
- (False) Loop terminates.
- Returns , which is .
Thus, the function returns .
Free practice · Your answers on this page are not saved.
