Question

needed in c++ (no step by step) The gcd(m, n) can also be defined recursively as...

needed in c++

(no step by step)

The gcd(m, n) can also be defined recursively as follows:

If m % n is 0, gcd (m, n) is n. Otherwise, gcd(m, n) is gcd(n, m % n).

Write a recursive function to find the GCD. Write a test program that prompts the user to enter two integers and displays their GCD.

Homework Answers

Answer #1

CODE IN C++:

#include <iostream>

using namespace std;
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);
return gcd(a, b-a);
}
int main()
{
int a, b ;
cout<<"Enter two numbers : ";
cin >> a >> b ;
cout << "Gcd of your numbers : " << gcd(a, b) << endl ;

return 0;
}

OUTPUT:

Know the answer?
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
Similar Questions
Write Java program Lab42.java which takes as input two positive integers m and n and computes...
Write Java program Lab42.java which takes as input two positive integers m and n and computes their least common multiple by calling method lcm(m,n), which in turn calls recursive method gcd(m,n) computing the greatest common divisor of m and n. Recall, that lcm(m,n) can be defined as quotient of m * n and gcd(m,n).
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
For any integer n > 0, n!(n factorial) is defined as the product n * n...
For any integer n > 0, n!(n factorial) is defined as the product n * n - 1 * n − 2 … * 2 * 1. And 0! is defined to be 1 Create function that takes n as input and computes then returns the accurate value for: n!= n * n - 1 * n − 2 … * 2 * 1 prompt the user to enter an integer n, call functions to compute the accurate value for...
Write a Python program that reads in the month, day, and year of a date and...
Write a Python program that reads in the month, day, and year of a date and prints it in the dd/mm/yyyy format or mm/dd/yyyy format, the format depending on the user’s preference. The program also prints the message It is a magic date If the product of month and day is equal to the last two digits of the year. For example, April 20 1980 is a magic date because April (numeric value 4) multiplied by 20 is equal to...
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an...
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an array to the function by reference. Here are two sample runs of the program. The elements of the array are: 0 4 5 6 7 Enter the element you want to find: 6 Element is in the array Press any key to continue . . .
1. A function f : Z → Z is defined by f(n) = 3n − 9....
1. A function f : Z → Z is defined by f(n) = 3n − 9. (a) Determine f(C), where C is the set of odd integers. (b) Determine f^−1 (D), where D = {6k : k ∈ Z}. 2. Two functions f : Z → Z and g : Z → Z are defined by f(n) = 2n^ 2+1 and g(n) = 1 − 2n. Find a formula for the function f ◦ g. 3. A function f :...
Write a C program that prompts the user to input as many unsigned(n) as the user...
Write a C program that prompts the user to input as many unsigned(n) as the user wants to type, terminated by non negative number You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a double You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a negative number. For each number the user types, output whether that number is prime or not. You won't need...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT