Problem in logic building

Please explain whole logic with code