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;
}
Discussion:
3 comments Page 1 of 1.
Prachi said:
8 years ago
Then what is the use of main ?
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.
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)
Sruthika said:
1 decade ago
Please explain this pogram.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers