Hello,
Even numbers are:
02
04
06
08
10
12
14
16
18
Odd numbers are:
01
03
05
07
09
11
13
15
17
19
Even numbers are:
02
04
06
08
10
12
14
16
18
20
22
24
26
28
Odd numbers are:
01
03
05
07
09
11
13
15
17
19
21
23
25
27
TCS has come up with hiring challenge for Mainframe professionals who are interested in pursuing an employment opportunity with them. I attempted the TCS Hiring Challenge on Dec 15th, 2018. It reminded me of those days I spent in Cognizant Academy, undergoing the training on Mainframes. To successfully pass the training, we had given tests which were very much similar to this challenge.
A day before the challenge, I received 2 emails from TCS. One with a sample question and the other one with instructions to join the challenge on Hackerrank. I used the latter email to join the challenge, the next day. Before starting the challenge, I was asked to agree to the consent that I'll not consult/copy code from any sources though I can refer manuals/use an IDE.
There were 2 questions which had to be finished within an hour. 1 for COBOL and the other one for SQL. I started the challenge with COBOL program.
The task was to write a COBOL program to display the even and odd numbers between two given numbers.
The program has to get 2 numbers from the user as input. Let's label the first input as LOWER and the 2nd input as UPPER. The code has to print the list of even and odd numbers between LOWER and UPPER.
For example, if LOWER and UPPER values are 01 and 19 respectively, the COBOL program should print the output in the following way:
Even numbers are:
02
04
06
08
10
12
14
16
18
Odd numbers are:
01
03
05
07
09
11
13
15
17
19
One constraint is that LOWER and UPPER should have values between 1 and 99.
I wrote the following code:
- The code accept two inputs from user. The input values are assigned to WS-LOWER and WS-UPPER data-items respectively.
- I preferred using 88 Conditional names for WS-LOWER and WS-UPPER data-items to validate the constraint. This helps me avoid coding lengthy IF condtions.
- Iterate between WS-LOWER and WS-UPPER using PERFORM loop.
- FUNCTION MOD, an intrinsic function in COBOL is being used to find whether a number is Even or odd. FUNCTION MOD outputs remainder which can be evaluated further.
- I've used tables with OCCURS DEPENDING ON to load the even and odd numbers and to display them as per the requirement.
Output: The below output was displayed for WS-LOWER and WS-UPPER values as 01 and 28 respectively.
Even numbers are:
02
04
06
08
10
12
14
16
18
20
22
24
26
28
Odd numbers are:
01
03
05
07
09
11
13
15
17
19
21
23
25
27
I preferred coding in COBOL Coding Ground by Tutorialspoint rather than coding in the IDE supported by Hackerrank. You just need to write a working piece of code in COBOL Coding Ground. Once done with code, you can copy and paste it in Hackerranks's COBOL IDE and compile it.
You can try my code in COBOL Coding Ground here.
Things to note when you're compiling the COBOL program in Hackerrank:
Be cautious about AREA A and AREA B and code accordingly.
Use period and END-PERFORM or END-IF, for that matter, to end the scope of a loop or conditional statement. For example, it is better to end PERFORM loop by coding END-PERFORM.
I took 50 minutes to get the COBOL code running and passing the test cases. With 10 minutes left for the contest to end, I moved on with the next question. Unfortunately, the SQL query was a lengthy one requiring INNER JOIN on 4 tables. The contest ended while I was typing the query. Ufff!
I received an email within a week from TCS saying I was not selected. Hmm, That's alright!
Hope you found this article useful. Share your thoughts in the comments section.