C++ Programming - Functions - Discussion
Discussion Forum : Functions - Programs (Q.No. 23)
23.
What will be the output of the following program?
#include<iostream.h>
class IndiaBix
{
int Num;
public:
IndiaBix(int x)
{
Num = x;
}
int BixFunction(void);
};
int IndiaBix::BixFunction(void)
{
static int Sum = 0;
int Dec;
Dec = Num % 10;
Num = Num / 10;
if((Num / 100)) BixFunction();
Sum = Sum * 10 + Dec;
return Sum;
}
int main()
{
IndiaBix objBix(12345);
cout<< objBix.BixFunction();
return 0;
}
Discussion:
5 comments Page 1 of 1.
Keerthu said:
1 decade ago
How 345 came ? please someone explain?
Vidya said:
1 decade ago
How the option C is correct ?explain.
Pavi said:
1 decade ago
x=12345.
sum=0.
dec=5.
num=1234.
Due to the next recursive call.
dec=4.
num=123.
In the next recursive call.
dec=3.
num=12.
When the sum is calculated.
sum=0*10+3 => sum=3.
sum=3*10+4 =>sum=34.
sum=34*10+5 =>sum=345.
sum=0.
dec=5.
num=1234.
Due to the next recursive call.
dec=4.
num=123.
In the next recursive call.
dec=3.
num=12.
When the sum is calculated.
sum=0*10+3 => sum=3.
sum=3*10+4 =>sum=34.
sum=34*10+5 =>sum=345.
Kartik said:
9 years ago
How if works, can anyone explain it, please?
Chanchla said:
9 years ago
Here the function call itself for 3 times.
1) x=12345.
sum=0.
dec=5.
num=1234.
Due to the next recursive call.
2) dec=4.
num=123.
In the next recursive call.
3) dec=3.
num=12.
Condition false 12/100
Then sum is calculated.
sum=0*10+3 => sum=3.
3 return to function
sum=3*10+4 =>sum=34.
34 value of sum return to function
sum=34*10+5 =>sum=345.
Return to main call.
1) x=12345.
sum=0.
dec=5.
num=1234.
Due to the next recursive call.
2) dec=4.
num=123.
In the next recursive call.
3) dec=3.
num=12.
Condition false 12/100
Then sum is calculated.
sum=0*10+3 => sum=3.
3 return to function
sum=3*10+4 =>sum=34.
34 value of sum return to function
sum=34*10+5 =>sum=345.
Return to main call.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers