Solution plus detailed explanation on Question 4 ( programming question ) of Class X ICSE 2018 Computer Applications boards paper.

source