C Program GCD By Recursion

By | March 10, 2020

C Program GCD By Recursion

/**
 * C program to find GCD that is HCF
 * of two numbers using recursion
 */
 
#include <stdio.h>

/* Function declaration */

int gcd(int a, int b);


int main()
{
    int n1, n2, result;
    
    /* Show message to enter two numbers
	and get input of two numbers from user
	*/
    
	printf("Enter two numbers to find GCD:\n");
    scanf("%d%d", &n1, &n2);
    
   	   
	result = gcd(n1, n2);
    
    printf("GCD of %d and %d = %d", n1, n2, result);
    
    return 0;
}


/**
 * Recursive function to find GCD of two numbers
 */
int gcd(int a, int b)
{
    
	if(b == 0)
        return a;
    else
        return gcd(b, a%b); 
}

Output:

Enter two numbers to find GCD:
54
45
GCD of 54 and 45 = 9

Loading

4 thoughts on “C Program GCD By Recursion

  1. Pingback: Recursion in Java Explained With Examples | EasyCodeBook.com

  2. Pingback: Python Recursive Function Check Palindrom String | EasyCodeBook.com

  3. Pingback: 4 C Programs Find GCD and LCM | EasyCodeBook.com

  4. Pingback: Python Recursion With Example Recursive Function | EasyCodeBook.com

Leave a Reply

Your email address will not be published. Required fields are marked *