Application Development and Automation 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: 
Read only

Answer required for this program

Former Member
0 Likes
839

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
Read only

Former Member
0 Likes
808

factorial of 5 is 120

6 REPLIES 6
Read only

Former Member
0 Likes
809

factorial of 5 is 120

Read only

0 Likes
808

Hi

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

Coz i am bit confused.

Thanks and Regards

Arun

Read only

0 Likes
808

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

Read only

0 Likes
808

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

Read only

0 Likes
808

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

Read only

Former Member
0 Likes
808

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.