C Programming - Functions - Discussion

Discussion Forum : Functions - Point Out Correct Statements (Q.No. 3)
3.
Which of the following statements are correct about the function?
long fun(int num)
{
    int i;
    long f=1;
    for(i=1; i<=num; i++)
        f = f * i;
    return f;
}
The function calculates the value of 1 raised to power num.
The function calculates the square root of an integer
The function calculates the factorial value of an integer
None of above
Answer: Option
Explanation:

Yes, this function calculates and return the factorial value of an given integer num.

Discussion:
19 comments Page 1 of 2.

Rohan said:   7 years ago
According to me, it is;

long fun(int num)
{
int i;
/* declare variable i as int.... */

long f=1;
/* declare and initialize variable f as long with value 1 */

for(i=1; i<=num; i++)
f = f * i;

/* for loop is used
Let's take an example...
-> 10 is given as arguments in the function fun...
[First Time]
- > i is initialized with 1 , 1<=10 ,condition true
- > f = 1*1
[Second Time]
- > i is incremented and becomes i = 2 , 2<=10 , condition true
- > f = 1*2 = 2

and further till i = 10, 10<=10 , condition true
- > f = 1* 2*3*4*5*6*7*8*9*10 */

return f;
/* because it returns an float variable .... */
}
(1)

Rohit said:   1 decade ago
@usha

If num =3 \* 3! = 6 *|
i=1 , f=1
Executes the for loop now i=1, 1<=3,post increment so i= 2 for next time

f= f*i =1*1 =1 --------returns f as 1
Next time after for loop
1*2 = 3-------------returns f as 2
After for loop
2*3 = 6 ---------------returns f as 6
Now for loops get failed in condition
So f=====6 \* ans *\

Praveena said:   9 years ago
Suppose num=4;

In the program given that
>> f=1
>>for(i=1;i<=4;i++)
f=f*i;
let us evaluate the for loop
step 1: f=1*1=1
step 2: f=1*2=2
step 3: f=2*3=6
step 4: f=6*4=24

IN EACH STEP VALUE OF " f " is saved

So factorial of 4 is 24.

Niks Wabale said:   7 years ago
It gives the correct result, even if we put 0 value then also for loop won't work and we return the value of f and previously it is initialized to 1 and 0!=1.

So it works perfectly fine for 0 value also.

Sanprince said:   6 years ago
You can take any value for num. In for loop, the statement written, it will behave like factorial so answer is C. Take any value you will understand.

Helloworld said:   9 years ago
It does not calculate the factorial of all the numbers. 0! is not calculated so None of the above must be the correct answer.

Hari said:   1 decade ago
How it calculate the factorial values here please explain step by step?

Sakshi munya said:   6 years ago
There is no 'main' function. Then, how it's execute?
(1)

Sreedhar said:   1 decade ago
In for loop, what is the value of num

Parth said:   1 decade ago
How it calculates factorial values?


Post your comments here:

Your comments will be displayed after verification.