Question

Give the output of the following C++ program and explain your reasoning: #include <iostream> using namespace...

Give the output of the following C++ program and explain your reasoning:

#include <iostream>

using namespace std;

int gcd( int , int ); //Function prototype

int main() {

int a = 105;

int b = 30;

cout << “The GCD of “ << a << “ and “ << b << “ is “ << gcd(a,b) << endl;

return 0; }

int gcd(int a, int b) {

if ( b == 0 )

return a;

else

return gcd(b,a%b);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

EXPLANATION -

FIRSTLY THE CODE IS USING FUNCTION DEFINATION AND EXPLANATION AT TWO DIFFERENT PLACES.

INT GCD(INT,INT) - GCD IS A NAME OF FUNCTIN AND IT TAKES TWO ARGUMENT THAT ARE ALSO INTEGERS . IT RETURNS AN ITEGER AS THE OUTPUT.

EXPLANATION OF GCD FUNCTION-

IT IS A RECURSIVE FUNCTION AND INITIALLY IT WILL TAKE 105 AND 30 AS ARGUMENT

IF B == 0 THEN THE GREATEST DIVISOR IS THE NUMBER ITSELF AS BECAUSE 0 IS DIVISIBLE BY EVERYTHING

NOW WHAT IS HAPPENING IS IT WILL REVERSE THE PARAMETER AND CALL THE FUNCTION

GCD(30,15) AS 105%30 = 15 BY DOING THIS THEY WILL GET TO KNOW THAT WHAT IS THE GREATEST NUMBER THAT DIVIDES BOTH

NOW AGAIN 15!=0 SO CALL FUNCTION GCD(15,0) AS 30%15==0

NOW AS B =0 THE OUTPUT IS 15

OUTPUT AND CODE-

OUTPUT IS 15

PROOF-

CODE

1 #include <iostream> 2 3 using namespace std; 4 5 int ged( int int ); //Function prototype 6 7 - int main() { 8 Rectangular

OUTPUT

the GCD of 105 and 30 is 15 ...Program finished with exit code 0 Press ENTER to exit console.

FEEL FREE TO ASK ANY DOUBT IN COMMENT SECTION.

Add a comment
Know the answer?
Add Answer to:
Give the output of the following C++ program and explain your reasoning: #include <iostream> using namespace...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT