Skip to content

Commit 9d0f323

Browse files
ShivaniThevarjrg94
andauthored
Added Even Odd in COBOL Article (#462)
* Added an article for Hello World in Rexx * Updated the article * Update authors.yml * Added Even Odd in COBOL Article * Edited the Article Co-authored-by: Jeremy Grifski <[email protected]>
1 parent 84bd491 commit 9d0f323

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Loading
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Even Odd in COBOL
3+
layout: default
4+
last-modified: 2021-10-16
5+
featured-image: even-odd-in-cobol-featured-image.jpg
6+
tags: [COBOL, even-odd]
7+
authors: [ShivaniThevar]
8+
---
9+
10+
In this article, we will learn how to check whether a number is even or odd in COBOL. Before doing that, it is preferred that you check out the article ["Hello World in COBOL"](https://sample-programs.therenegadecoder.com/projects/hello-world/cobol/) to better understand the basic structure of a COBOL program.
11+
12+
## How to Implement the Solution
13+
14+
Let us first look at the code:
15+
16+
```COBOL
17+
IDENTIFICATION DIVISION.
18+
PROGRAM-ID. EVEN-ODD.
19+
DATA DIVISION.
20+
WORKING-STORAGE SECTION.
21+
01 CMDARGS PIC X(38).
22+
01 NUM PIC S9(30).
23+
PROCEDURE DIVISION.
24+
ACCEPT CMDARGS FROM COMMAND-LINE.
25+
IF CMDARGS IS ALPHABETIC THEN
26+
DISPLAY "Usage: please input a number"
27+
STOP RUN.
28+
COMPUTE NUM = FUNCTION NUMVAL(CMDARGS).
29+
IF NUM IS NUMERIC THEN
30+
IF FUNCTION MOD (NUM, 2) = 0 THEN
31+
DISPLAY "Even"
32+
ELSE
33+
DISPLAY "Odd"
34+
ELSE
35+
DISPLAY "Usage: please input a number"
36+
STOP RUN.
37+
```
38+
39+
Let us try to understand the code step-wise.
40+
41+
First, we must make sure we include the mandatory `DIVISIONS` in the beginning. The `IDENTIFICATION DIVISION` is used to specify the metadata of the program including the `PROGRAM-ID`. In this case, we have written EVEN-ODD to identify the code.
42+
43+
Next comes the `DATA DIVISION`. Here, we define our data variables, which will contain data like integer, alphabets etc. Here, we have mentioned `CMDARGS` and `NUM` which are short for Command Arguments and Number respectively. `PIC` stands for 'Picture Clause' which defines the type and size of data. `X` means an Alpha-numeric data can be expected in Command Arguments (maximum size is 38 bytes). We have used alphanumeric because the user can also enter alphabets instead of a number. On the other hand, `S9` shows that a number with positive or negative values can be expected as data (maximum size is 30 bytes).
44+
45+
Moving on, we encounter `PROCEDURE DIVISION`. This section includes the actual code with its functions. To solve this problem, we will use a computing operator 'Modulo' (%) which divides one number from the other and returns the remainder. So, if we divide any even number with 2, the remainder will always be 0, whereas, if we divide any odd number with 2, the remainder would be 1.
46+
47+
For example, 12 % 2 = 0 and 13 % 2 = 1.
48+
49+
`ACCEPT` is a keyword used to accept arguments (data) from the Command-Line. So, this statement is used to accept the data entered by the user and run it through the code.
50+
51+
If command arguments are alphabetic, the program will display the error message `Usage: please input a number` and stop the program.
52+
53+
`NUMVAL` extracts numeric data from an alphanumeric data and stores them.
54+
55+
Next, we have nested (one inside the other) IF conditions. `IF NUM IS NUMERIC THEN` is used to make sure again that we are only using a numeric data to print an output. So, if the data entered is a number, the program will go ahead with this condition and the `MOD` (modulo) operator will be applied on the number.
56+
57+
`MOD (NUM, 2) = 0 THEN DISPLAY "Even"`. If the remainder is 0, then the number is even and so is the output.
58+
59+
If this condition is not true, i.e., if the remainder is not 0, the output will be Odd.
60+
61+
And finally, if the data is not a number, the output will be an error message "Usage: please input a number".
62+
63+
## How to Run the Solution
64+
65+
To run the solution we will need [a COBOL compiler](https://gnucobol.sourceforge.io/) installed and [the actual code file](https://github.com/TheRenegadeCoder/sample-programs/blob/main/archive/c/cobol/even-odd.cbl).
66+
Finally we need to run these commands in order:
67+
68+
```console
69+
cobc -x even-odd.cbl
70+
$ ./even-odd
71+
```
72+
The commands first compile the source code into an executable and then execute it.
73+
Alternatively, you might want to use an [online COBOL compiler](https://www.jdoodle.com/execute-cobol-online/)

0 commit comments

Comments
 (0)