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: 

Answer required for this program

Former Member
0 Kudos

Hi all

can anyone gimme the answer for this program

REPORT demo_mod_tech_example_2.

DATA: num TYPE i VALUE 5,

fac TYPE i VALUE 0.

PERFORM fact USING num CHANGING fac.

WRITE: / 'Factorial of', num, 'is', fac.

FORM fact

USING value(f_num) TYPE i

CHANGING f_fact TYPE i.

f_fact = 1.

WHILE f_num GE 1.

f_fact = f_fact * f_num.

f_num = f_num - 1.

ENDWHILE.

ENDFORM.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

factorial of 5 is 120

6 REPLIES 6

Former Member
0 Kudos

factorial of 5 is 120

0 Kudos

Hi

can u please explain the how u got the right answer??

Coz i am bit confused.

Thanks and Regards

Arun

0 Kudos

Hi Arun,

Just put a break point in the subroutine and check out what is happening

Check out the values of the variables in debugging

you will get to know

0 Kudos

Hi chandrasekar,

I know that i can debug and found it out.

I dont have access to SAP System.. if i got system i would have never raised this query.

Hope u understand.

Thanks,

Arun

0 Kudos

Factorial of 5 = 5 * 4 * 3 * 2 * 1

Former Member
0 Kudos

In your form the value of f_num is 5(ie, default value) at first time and value of f_fact = 1.

and inwhile loop.

1st iteration

f_fact = f_fact * f_num. ie, 1* 5 = 5.

f_num = f_num - 1. ie, 5 -1 =4

2nd iteration

f_fact = f_fact * f_num. ie, 5* 4 = 20.

f_num = f_num - 1. ie, 4 -1 =3

3rd iteration

f_fact = f_fact * f_num. ie, 20* 3 = 60.

f_num = f_num - 1. ie, 3 -1 =2

4thiteration

f_fact = f_fact * f_num. ie, 60* 2 = 20.

f_num = f_num - 1. ie, 2 -1 = 1

Now control control reaches Endwhile as f_num not greater than 1 and returns the value of f_num(ie,120) to fac.