Determining even/odd values and counting them

Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Determining even/odd values and counting them

Postby hakghen » Sun Jun 28, 2009 7:45 am

Sorry to bug again, but I need just another help on a specific procedure...
I'm reading some random numbers from the SYSIN and I wanna get:

The greatest odd number;
The greatest even number;
The number of odd numbers;
The number of even numbers;

These are the declared variables:

        DATA DIVISION.                         
        WORKING-STORAGE SECTION.               
        01 WS-NUMERO        PIC 9(3).           
        01 WS-TOTAL-PAR     PIC 9(3) VALUE ZERO.
        01 WS-TOTAL-IMPAR   PIC 9(3) VALUE ZERO.
        01 WS-REMAINDER     PIC 9.             
        01 WS-PAR-MAIOR     PIC 9(3) VALUE ZERO.
        01 WS-PAR-MENOR     PIC 9(3) VALUE 999.
        01 WS-IMPAR-MAIOR   PIC 9(3) VALUE ZERO.
        01 WS-IMPAR-MENOR   PIC 9(3) VALUE 999.
        01 WS-RESULT        PIC 9(3).           

And this is the procedure to find the things I want:

          ACCEPT WS-NUMERO FROM SYSIN.                               
          DIVIDE WS-NUMERO BY 2                                       
                 GIVING WS-RESULT                                     
                 REMAINDER WS-REMAINDER.                             
          IF WS-REMAINDER EQUAL ZERO                                 
             AND WS-NUMERO GREATER WS-PAR-MAIOR                       
             ADD 1 TO WS-TOTAL-PAR                                   
             MOVE WS-NUMERO TO WS-PAR-MAIOR                           
             IF WS-NUMERO LESS WS-PAR-MENOR                           
             MOVE WS-NUMERO TO WS-PAR-MENOR                           
             ADD 1 TO WS-TOTAL-IMPAR                                   
             IF WS-NUMERO GREATER WS-IMPAR-MAIOR                       
                MOVE WS-NUMERO TO WS-IMPAR-MAIOR                       
             IF WS-NUMERO LESS WS-IMPAR-MENOR                         
                MOVE WS-NUMERO TO WS-IMPAR-MENOR                       

Also, if it isn't too much to ask, I wanna do something like, insert on the display the DATE and TIME when the program was run... Any idea of how I should do that and then format the date and time exit to show as this: HH:MM:SS and YYYY/MM/DD?

Thanks for the attention and help! ;)

User avatar
Posts: 59
Joined: Thu Sep 11, 2008 8:15 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Determining even/odd values and counting them

Postby dick scherrer » Sun Jun 28, 2009 9:44 am


When you have a new question, you need to start a new topic.

One way to get the system date and time is using the ACCEPT: ... lr40/6.2.1?

This should already be bookmarked or downloaded, so i'll simply refer it in the future.

You need to change/remove ACCEPT SYSIN. What you need to do is define a file (FD), open it, and read it until end-of-file to obtain the input values. You should also make sure the input value is numeric before using it. Invalid values could be didcarded or the run simply terminated with an appropriate message.

What happened when you compiled/ran this?
Hope this helps,
User avatar
dick scherrer
Global moderator
Posts: 6269
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Return to IBM Cobol


  • Related topics
    Last post