C++ Programming - Functions - Discussion

Discussion Forum : Functions - Programs (Q.No. 38)
38.
What will be the output of the following program?
#include<iostream.h> 
class Number
{
    int Num; 
    public:
    Number(int x = 0)
    {
        Num = x;
    }
    void Display(void)
    {
        cout<< Num;
    }
    void Modify(); 
};
void Number::Modify()
{
    int Dec;
    Dec = Num % 13; 
    Num = Num / 13; 
    
         if(Num  > 0 ) Modify()   ; 
         if(Dec == 10) cout<< "A" ; 
    else if(Dec == 11) cout<< "B" ; 
    else if(Dec == 12) cout<< "C" ; 
    else if(Dec == 13) cout<< "D" ;
    else               cout<< Dec ;
}
int main()
{
    Number objNum(130);
    objNum.Modify();
    return 0; 
}
130
A0
B0
90
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
6 comments Page 1 of 1.

Aaditya Gupta said:   10 years ago
How A0 is printed? Display function is not called here.

Eliott said:   7 years ago
Wouldn't Dec = 0 the first time since 130 % 13 = 0? Please tell me.

Neha said:   7 years ago
Here if(num>0) is satisfied if num = 0.

So,

if(0>0)condition is false the else part of that if is called and value of Dec is printed then next if is exicuted (Dec==10)then A is printed.

Parikshit said:   7 years ago
Can anyone please explain how this program is working?

Baku said:   5 years ago
When Modify function is first called,
//Num = 130
Dec = Num %13; // Dec = 0
Num = Num / 13; // Num =10
if(Num > 0 ) Modify() ; // 10 > 0 so call modify function within the same class once again (recursive)

Inside Second Modify call:
int Dec; // new formal parameter with garbage value
Dec = Num % 13; // value of num has been changed previously Num is now 10 so Dec = 10
Num = Num / 13; // Num = 0
if(Num > 0 ) Modify(); // 0 > 0 so no call recursion stops
if(Dec == 10) cout<< "A" ; // print A and exit out of Modify function to

Finish first called modify function:
Inside Modify 1.
Jumping to last else part as all the if and else if conditions become false.
cout<< Dec ; // print 0.

Snehal said:   3 years ago
@All.

What will be the output of the following program? Write the answer in digits not an alphabet.

#include<iostream>
using namespace std;
class Base
{
int x, y;
public:
Base(int xx = 10, int yy = 10)
{
x = xx;
y = yy;
}
void Show()
{
cout<< x * y << endl;
}
};
class Derived : public Base
{
private:
Base objBase;
public:
Derived(int xx, int yy) : Base(xx, yy), objBase(yy, yy)
{
objBase.Show();
}
};
int main()
{
Derived objDev(10, 20);
return 0;
}

Post your comments here:

Your comments will be displayed after verification.