C++ Programming - Functions - Discussion

Discussion Forum : Functions - Programs (Q.No. 16)
16.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
long FactFinder(long = 5); 
int main()
{
    for(int i = 0; i<= 0; i++)
        cout<< FactFinder() << endl; 
    return 0;
}
long FactFinder(long x)
{
    if(x < 2)
        return 1; 
    long fact = 1; 
    for(long i = 1; i <= x-1; i++)
        fact = fact * i; 
    return fact; 
}
The program will print the output 1.
The program will print the output 24.
The program will print the output 120.
The program will print the output garbage value.
The program will report compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
3 comments Page 1 of 1.

Sruthika said:   1 decade ago
Please explain this pogram.

Ashwak said:   1 decade ago
In the main(),

i=0, hence i<=0 is true. so FactFinder() would be called.

In FactFinder(),

x=5 (default)
if(5<2) is false. so the controll goes to the loop and executes for four times.

1.fact=1*1; (fact=1)
2.fact=1*2; (fact=2)
3.fact=2*3; (fact=6)
4.fact=6*4; (fact=24)

Hence the answer 24.
(1)

Prachi said:   8 years ago
Then what is the use of main ?

Post your comments here:

Your comments will be displayed after verification.