Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,158 questions

40,712 answers

573 users

How to calculate the GCD (greatest common divisor) of two integers using recursion in C

1 Answer

0 votes
#include <stdio.h> 

int getGCD_Recursion(int a, int b) {
    while (a != b) {
        if (a > b) {
            return getGCD_Recursion(a - b, b);
        }
        else {
            return getGCD_Recursion(a, b - a);
        }
    }
    return a;
}
  
int main(void)
{   
    int a = 12, b = 20;
      
    printf("The GCD (greatest common divisor) of %d and %d is: %d\n", a, b, getGCD_Recursion(a, b));
 
    return 0;
}
  
  
   
/*
run:
 
The GCD (greatest common divisor) of 12 and 20 is: 4
  
*/

 





answered Jan 17, 2021 by avibootz
...