Answer to Question #242680 in C++ for felo

Question #242680

Write a program that enable one to key in any 4 numbers and determine their GCD.

1
Expert's answer
2021-09-27T04:52:52-0400
#include <iostream>

int gcd(int a, int b)
{
  if (a == 0) 
    return b;
  if (b == 0) 
    return a;
  if (a == b) 
    return a;
  if (a > b)
    return gcd(a-b, b);
  if (a < b)
    return gcd(a, b-a);
}

int main() 
{
  int a, b, c, d;
  
  std::cout << "Enter any positive four numbers:" << std::endl;
  std::cin >> a >> b >> c >> d;
  std::cout << "GCD(" << a << ", " << b << ", " << c << ", " << d <<") = " << gcd(gcd(a, b), gcd(c, d)) << std::endl;)
  
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS