Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

how to get the maximum of 3 numbers

Former Member
0 Kudos
2,289

Hi,

Say I have num1, num2, num3, is there any way I could use a function to get the maximum of 3 numbers instead of writing all the if loops? Thanks a lot!

Regards,

Anyi

1 ACCEPTED SOLUTION

Former Member
0 Kudos
303

Create a function with the below code:

FUNCTION zmax_number.

*"----


""Local Interface:

*" IMPORTING

*" VALUE(NUM1) TYPE I OPTIONAL

*" VALUE(NUM2) TYPE I OPTIONAL

*" VALUE(NUM3) TYPE I OPTIONAL

*" EXPORTING

*" REFERENCE(MAX) TYPE I

*"----


IF num1 GT num2.

max = num1.

ELSE.

max = num2.

ENDIF.

IF num3 GT max.

max = num3.

ENDIF.

ENDFUNCTION.

3 REPLIES 3

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
303

You can add the values to a itab then sort in descending order.




report zrich_0001.

data: num1 type i value '2'.
data: num2 type i value '3'.
data: num3 type i value '1'.

Data: itab type table of string with header line.

itab = num1. append itab.
itab = num2. append itab.
itab = num3. append itab.

sort itab descending.
read table itab index 1.
If sy-subrc = 0.
 Write:/ 'The greatest number is', itab.
endif.


Regards,

Rich Heilman

0 Kudos
303

Hi Rich,

Can i use the same code as Customer Exit Variable to resolve below issue(Reporting Requirment)

My requirment is to develop a report to show all emp whose policy is going to expire ith in the user selected date intreval...

Emp_ID--App_Date-Policy_Strt_Date--Policy_End_Date--App_Status

1


01.01.2007---Blank
Blank
--


NEW (New application for a Medical Insurance policy raised for employee)

1


01.01.2007---01.02.2007
30.05.2007
--


COMPLETE (policy issued for emp with strt & end dates)

1


15.05.2007---01.02.2007
30.05.2007
--


RENEWAL (a application is raised to renew emp policy)

1


15.05.2007---01.06.2007
31.12.2007
--


COMPLETE (policy issued for emp with strt & end dates)

1


15.12.2007---01.06.2007
31.12.2007
--


RENEWAL (a application is raised to renew emp policy)

1


15.12.2007---01.01.2008
31.05.2008
--


COMPLETE (policy issued for emp with strt & end dates)

1


15.05.2008---01.01.2008
31.05.2008
--


RENEWAL (a application is raised to renew emp policy)

1


15.05.2008---01.06.2008
31.05.2009
--


COMPLETE (a application is raised to renew emp policy)

So EMP 1 got valid policy untill 31.05.2009.

When a user executes a query Inputs a date range (variable on Expiry Date) to show all employees whose policy expiry date intreval (01.05.2008 to 31.05.2008)

it shouldn't show Emp 1 as he got valid policy untill 31.05.2009 but currently it is showing Emp 1....

How can i achieve this

Thanks

Former Member
0 Kudos
304

Create a function with the below code:

FUNCTION zmax_number.

*"----


""Local Interface:

*" IMPORTING

*" VALUE(NUM1) TYPE I OPTIONAL

*" VALUE(NUM2) TYPE I OPTIONAL

*" VALUE(NUM3) TYPE I OPTIONAL

*" EXPORTING

*" REFERENCE(MAX) TYPE I

*"----


IF num1 GT num2.

max = num1.

ELSE.

max = num2.

ENDIF.

IF num3 GT max.

max = num3.

ENDIF.

ENDFUNCTION.