Logical error

Can anybody tell what logic is required in this i have tried everything but still it fails in one condition


and this is question

another method that i have tried is