MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

number is even or odd
Goto page 1, 2  Next
 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming
View previous topic :: View next topic  
Author Message
rakshith
Beginner


Joined: 26 Jul 2005
Posts: 32
Topics: 16
Location: bangalore

PostPosted: Mon Dec 26, 2005 4:24 am    Post subject: number is even or odd Reply with quote

hi all,

Is there any standard logic to determine the single digit number is even number or odd number, if it is there please post here,


thanks in advance.

rakshith
Back to top
View user's profile Send private message Yahoo Messenger
mask-of-zorro
Beginner


Joined: 19 Nov 2005
Posts: 23
Topics: 10

PostPosted: Mon Dec 26, 2005 4:47 am    Post subject: Reply with quote

rakshith wrote:
hi all,

Is there any standard logic to determine the single digit number is even number or odd number, if it is there please post here,


thanks in advance.

rakshith
Quote:

standard logic to determine the single digit number is even number or odd number



as per what u had mentioned its a single digit ...

one logic is Divide it by 2 and check the remainder

else u can do the below one

being a single digit check it whether its either '0' or '2' or '4' or '6' or '8'.


this is a general logic that can be done in most of the programming languages


_________________________________________________________


" No one is listening until you make a mistake"
Back to top
View user's profile Send private message
shekar123
Advanced


Joined: 22 Jul 2005
Posts: 528
Topics: 90
Location: Bangalore India

PostPosted: Mon Dec 26, 2005 8:04 am    Post subject: Reply with quote

rakshith,

Try this code.

Code:

WORKING-STORAGE SECTION.
01 WS-NUM     PIC 9(1) VALUE ZERO.                 
01 WS-Q       PIC 9(1) VALUE ZERO.                 
01 WS-R       PIC 9(1) VALUE ZERO.                 
PROCEDURE DIVISION.                                 
0000-MAIN.                                         
    ACCEPT WS-NUM.                                 
    DISPLAY 'WS-NUM IS ' WS-NUM.                   
    DIVIDE WS-NUM INTO 2 GIVING WS-Q REMAINDER WS-R.
    IF WS-R = 0                                     
       DISPLAY 'NUMBER IS EVEN ' WS-NUM             
    ELSE                                           
       DISPLAY 'NUMBER IS ODD  ' WS-NUM             
    END-IF.                                         


OUTPUT

Code:

WS-NUM IS 9     
NUMBER IS ODD  9


_________________
Shekar
Grow Technically
Back to top
View user's profile Send private message
iknow
Beginner


Joined: 09 Jan 2006
Posts: 39
Topics: 15
Location: Inside the MATRIX

PostPosted: Tue Jan 10, 2006 4:58 am    Post subject: Reply with quote

Hi All,


As a follow up to the above post, Could anyone help me in finding whether the given number is odd or even without using any conditional statements.


Please help me.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12370
Topics: 75
Location: San Jose

PostPosted: Tue Jan 10, 2006 5:06 am    Post subject: Reply with quote

Quote:

As a follow up to the above post, Could anyone help me in finding whether the given number is odd or even without using any conditional statements.


iknow,

Care to elaborate a bit more?

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
iknow
Beginner


Joined: 09 Jan 2006
Posts: 39
Topics: 15
Location: Inside the MATRIX

PostPosted: Tue Jan 10, 2006 5:29 am    Post subject: Reply with quote

Hi Kolusu,

What I was looking for exactly is I need a logic to find out whether a given number is even or odd.

I must not use condition statements, PERFORM.

Is it possible through arrays or by looking at the LSB of the given number.

Hope I reached you.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12370
Topics: 75
Location: San Jose

PostPosted: Tue Jan 10, 2006 5:47 am    Post subject: Reply with quote

Quote:

I must not use condition statements, PERFORM.

Is it possible through arrays or by looking at the LSB of the given number


How do you expect to check Least Significant Bit/Byte without a condition statement? and why all these restrictions? .

A Single IF statement solves the purpose and why do want to make it complicated?

Thanks

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
iknow
Beginner


Joined: 09 Jan 2006
Posts: 39
Topics: 15
Location: Inside the MATRIX

PostPosted: Tue Jan 10, 2006 6:26 am    Post subject: Reply with quote

Hi Kolusu,

You can check the LSB manually by converting the number to Binary.

The requirement is I should not use conditional statements.


Please help me.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12370
Topics: 75
Location: San Jose

PostPosted: Tue Jan 10, 2006 8:03 am    Post subject: Reply with quote

Quote:

You can check the LSB manually by converting the number to Binary.


iknow,

Enlighten me please.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
iknow
Beginner


Joined: 09 Jan 2006
Posts: 39
Topics: 15
Location: Inside the MATRIX

PostPosted: Tue Jan 10, 2006 8:21 am    Post subject: Reply with quote

Hi Kolusu,

For example,

Say number 'x' to be checked for odd or even.

Convert the 'X' to binary and check if the LSB of that number is zero then we can say that the number is even.

Say '4' to be checked.

Equivalent binary form of 4 is 100. So LSB is '0' in this case so I can say the given number is even.

Hope I reached you.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12370
Topics: 75
Location: San Jose

PostPosted: Tue Jan 10, 2006 8:39 am    Post subject: Reply with quote

Iknow,

*Sigh* I am still waiting to see as to how you would implement the conversion to binary and checking the least significant byte WITHOUT using conditional statements.

Kolusu

PS: If you think you are smart and prove it over here , you are knocking on the wrong door.
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
iknow
Beginner


Joined: 09 Jan 2006
Posts: 39
Topics: 15
Location: Inside the MATRIX

PostPosted: Tue Jan 10, 2006 8:43 am    Post subject: Reply with quote

Hi Kolusu,


Do the conversion process manually.

Hope I answered your query.
Back to top
View user's profile Send private message
Mervyn
Moderator


Joined: 02 Dec 2002
Posts: 415
Topics: 6
Location: Hove, England

PostPosted: Tue Jan 10, 2006 10:12 am    Post subject: Reply with quote

Using shekar123's code as a basis, you could do something like this with 88 levels:

Code:
WORKING-STORAGE SECTION.
01 WS-NUM     PIC 9(1) VALUE ZERO. 
   88 EVEN-NUMBER      VALUES 0,2,4,6,8.
   88 ODD-NUMBER       VALUES 1,3,5,7,9.               
             
PROCEDURE DIVISION.                                 
0000-MAIN.                                         
    ACCEPT WS-NUM.                                 
    DISPLAY 'WS-NUM IS ' WS-NUM.                   
    IF EVEN-NUMBER                                     
       DISPLAY 'NUMBER IS EVEN ' WS-NUM             
    ELSE                                           
       DISPLAY 'NUMBER IS ODD  ' WS-NUM             
    END-IF.                                         


but you're still going to need an "IF" in there, or maybe an "EVALUATE".

I'd like to see your Cobol solution.
_________________
The day you stop learning the dinosaur becomes extinct
Back to top
View user's profile Send private message
iknow
Beginner


Joined: 09 Jan 2006
Posts: 39
Topics: 15
Location: Inside the MATRIX

PostPosted: Wed Jan 11, 2006 1:21 am    Post subject: Reply with quote

Hi All,

Here is my solution to odd or even problem without using conditional statements.

Please rate my answer.


[code:1:b1b7da0d29]WORKING-STORAGE SECTION.
01 WS-NUM PIC 9(1) VALUE ZERO.

01 WS-DISP-VAL

05 DISP-VAL PIC X(4) OCCURS 2 TIMES
.



PROCEDURE DIVISION.
0000-MAIN.
ACCEPT WS-NUM.

MOVE
Back to top
View user's profile Send private message
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 699
Topics: 63
Location: USA

PostPosted: Wed Jan 11, 2006 3:56 am    Post subject: Reply with quote

iknow,

It seems you already knew the solution, so it would have been a good post for 'Mainframe Challenge'.

Regards,
Diba.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group