Kindly tell me how could this be done in COBOL.
By writing code. Start by writing pseudocode that does what you explained, then convert the pseudocode into COBOL statements. Since this is a HELP forum, not a WRITE-YOUR-CODE-FOR-YOU forum, we won't write the code for you (unless we get paid, of course -- 1000 U.S. dollars or equivalent is a typical rate). We will, however, help steer you correctly when you post some indication that you've made progress but cannot figure out where your code is going awry.