Cognizant Interview Question

Program logics for a questions