Question

Functions & Arrays Q1) The greatest common divisor (GCD) of two Integers (of which at least one is nonzero) is the largest po

coding in c programming

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

Code :

#include <stdio.h>

int gcd(int a, int b){ //functiom that take two number as input and return gcd of these two number
if(a==0 && b==0){ //checking case when both number is zero
return -1;
}

if (a == 0) {
if(b<0){
return -b;
}
else{
return b;
}   
}

if (b == 0){
if(a<0){
return -a;
}
else{
return a;
}
}

return gcd(b,a%b); //recursive calling again calling gcd

}

int main() {

int num1,num2,res;
printf("Enter two number: "); //taking input from users
scanf("%d%d",&num1,&num2);

res = gcd(num1,num2); //calling gcd function

if(res!=-1){
printf("GCD of %d and %d is %d\n",num1,num2,res);//printing gcd of two number
}
else{
printf("At least one number must be nonzero\n");//printing message when both number are zero
}
return 0;

}

Screenshot :

Code :

1 2 HomeworkLib.c #include <stdio.h> int gcd(int a, int b){ //function that take two number as input and return god of these two nu

Output:

C:\Users\karju\Documents\HomeworkLib.exe Enter two number: 00 At least one number must be nonzero Process exited after 12.27 secondC:\Users\karju\Documents\HomeworkLib.exe Enter two number: 16 8 GCD of 16 and 8 is 8 Process exited after 6.697 seconds with return

Add a comment
Know the answer?
Add Answer to:
coding in c programming Functions & Arrays Q1) The greatest common divisor (GCD) of two Integers...
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
  • I want the code in C++ The greatest common divisor (GCD) of two integers is the...

    I want the code in C++ The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a function called GCD that has a void return type, and accepts 3 parameters (first two by value, third by reference). The function should find the greatest common divisor of the first two numbers, and have the result as its OUTGOING value. Write a main function that asks the users for two integers, and...

  • PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer...

    PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides the two numbers without a remainder. For example, the GCD of 8 and 12 is 4. Steps to calculate the GCD of two positive integers a,b using the Binary method is given below: Input: a, b integers If a<=0 or b<=0, then Return 0 Else, d = 0 while a and b are both even do a = a/2 b = b/2...

  • Consider the problem of finding the Greatest Common Divisor (GCD) of two positive integers a and...

    Consider the problem of finding the Greatest Common Divisor (GCD) of two positive integers a and b. It can be mathematically proved that if b<=a GCD(a, b) = b, if (a mod b) = 0; GCD(a, b) = GCD(b, a mod b), if (a mod b) != 0. Write a recursive function called GCD with signature “public static int GCD(int a, int b)” that returns the greatest common divisor of two positive integers a and b with b <= a....

  • Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g...

    Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g edi ) and gcdr , which both take two integers a, b and calculates their greatest common divisor (GCD) using the Euclidean algorithm gcdi () should do so using iteration while gcdr () should use recursion. Then write a third function, gcd(), which takes two integers a, band an optional third argument nethod which takes a charater string containing either "iterative" or "recursive", with...

  • 1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where...

    1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where a 2 b is equal to the greatest common divisor of b and (a mod b). Write a program that implements this algorithm to find the GCD of two integers. Assume that both integers are positive. Follow this algorithm: 1. Call the two integers large and small. 2. If small is equal to 0: stop: large is the GCD. 3. Else, divide large by...

  • Write a C++ program that reads in two integers and then computes the greatest common divisor...

    Write a C++ program that reads in two integers and then computes the greatest common divisor GCD using recursion. DO NOT USE A BUILT-IN FUNCTION SUCH AS "gcd" to do this.

  • C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

    C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...

  • IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd)...

    IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an...

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

    Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result "Euclid's Greatest Common Divisor Algorithm" GCD(M,N) = M                      (if N is 0) GCD(M,N) = GCD(N, M % N)   (if N > 0) you may assume that inputs are non-negative name your assembly...

  • C++ I do not understand this problem. 14THE GREATEST COMMON DENOMINATOR PROBLEM Write a function named...

    C++ I do not understand this problem. 14THE GREATEST COMMON DENOMINATOR PROBLEM Write a function named god() that accepts two integers as input parameters and returns the greatest common divisor of the two numbers. The greatest common divisor (GCD) of two integers, a and b, is the largest integer that is a factor of both a and b. The GCD of any number and 1 is 1, and the GCD of any number and o is that number. One efficient...

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