Skip to content
Exercise · Q18

Q.For some calculations, we want an algorithm that accepts only positive integers upto 100.
Accept_1to100_Algo
INPUT Number
IF (0<= Number) AND (Number <= 100)
ACCEPT
Else
REJECT

a) On what values will this algorithm fail?
b) Can you improve the algorithm?
ChseodishaTextbookSubjective· 3mImportance★★★★★est
100% · 36/36 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The check 0 <= Number <= 100 wrongly accepts 0 (not positive) and wrongly accepts fractions like 2.5 (not integers); the corrected condition is "Number is an integer AND 1 <= Number <= 100".

The idea (matching the condition to the specification). The specification is precise: accept positive integers up to 100, i.e. exactly the whole numbers 1, 2, …, 100. Verifying an algorithm means comparing what the condition actually admits against that set — every extra value admitted is a failure case.

a) On what values does it fail?

InputAlgorithm's verdictCorrect verdictFailure?
00 <= 0 <= 100 → ACCEPTreject (0 is not positive)yes
2.50 <= 2.5 <= 100 → ACCEPTreject (not an integer)yes
99.99ACCEPTreject (not an integer)yes
-50 <= -5 false → REJECTrejectno
100ACCEPTacceptno
101REJECTrejectno

So the algorithm fails on 0 (the lower bound is wrong — it should start at 1) and on all non-integer values between 0 and 100 (there is no integrality check at all).

b) Improved algorithm

Accept_1to100_Algo (improved)
INPUT Number
IF (Number is an integer) AND (1 <= Number) AND (Number <= 100)
     ACCEPT
Else
     REJECT

Python verification

def accept(number):
    if number == int(number) and 1 <= number <= 100:
        return "ACCEPT"
    return "REJECT" …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.