Answer to Question #242677 in C++ for felo

Question #242677

Write a program to key in any four numbers and determine the G.C.D.

1
Expert's answer
2021-09-26T18:49:57-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