Question

Write a complete C++ program to ask the user to inter two integers and the program finds and display the greatest common divisor (GCD) of the two integers. Typical output screen should be as following:
Write a complete C++ program to ask the user to inter two integers and the program finds and display the greatest common divi

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

Program:

#include <iostream>

using namespace std;

// function definition of findGcd
int findGcd(int a, int b) 
{ 
    // Everything divides 0  
    if (a == 0) 
    
       return b; 
       
    if (b == 0) 
    
       return a; 
   
    // if a is equal to b then gcd is same(a or b)
    if (a == b) 
    
        return a; 
   
    // if a is greater than b then recursively call findGcd with parameter a-b and b
    if (a > b) 
    
        return findGcd(a-b, b); 
     
    // recursively call findGcd with parameter a and b-a
    return findGcd(a, b-a); 
} 

int main()

{
    
    // Integer veritable a and b is to store two integers
    int a, b;
    
    cout<<"Enter two integers";
    
    // read first integer to veritable a
    cin >> a;
    
    // read first integer to veritable b
    cin >> b;
    
    // call the function findGcd with a and b as parameters, to calculate gcd and store the result in veriable gcd
    int gcd = findGcd(a, b);
    
    // print the content in variable gcd
    cout << "The GCD is - " << gcd;
    
    return 0;
    
}

Screenshot:

main.cpp 8 9 #include <iostream> 10 11 using namespace std; 12 13 // function definition of findGcd 14 int findGcd(int a, int

38 39 int main() 40 41 - { 42 43 // Integer veritable a and b is to store two integers 44 int a, b; 45 46 cout<<Enter two in

Enter two intigers 18 27 The GCD is - 9 ... Program finished with exit code 0 Press ENTER to exit console. I

Add a comment
Know the answer?
Add Answer to:
Write a complete C++ program to ask the user to inter two integers and the program...
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