Monday , September 25 2017
Home / c-program / C Program – Find GCD of two numbers

C Program – Find GCD of two numbers

C Program to find the GCD of two numbers.

GCD of two numbers :


#include<stdio.h> 
int gcdCalc(int a,int b); 
int main(void) 
{ 
    int x, y; 
    printf("Enter x and y values to calculate GCD : \n"); 
    scanf("%d%d",&x, &y); 
    printf("%d\n",gcdCalc(x,y)); 
    return 0; 
} 
 
int gcdCalc(int a,int b) 
{ 
    int rem; 
    while(b!=0)  
    { 
        rem=a%b; 
        a=b; 
        b=rem; 
    } 
    return a; 
}

Enter x and y values to calculate GCD :
25
60
5

Enter x and y values to calculate GCD :
800
100
100

GCD of two numbers Recursive :


#include<stdio.h> 
int GCD_Recursive(int a,int b); 
int main(void) 
{ 
    int x, y; 
        printf("Enter x and y values to calculate GCD : \n"); 
        scanf("%d%d",&x, &y); 
        printf("%d\n",GCD_Recursive(x,y)); 
        return 0;  
}
 
int GCD_Recursive(int a,int b)    
{ 
    if(b==0) 
        return a; 
    return GCD_Recursive(b, a%b); 
}

Enter x and y values to calculate GCD :
800
100
100

Enter x and y values to calculate GCD :
350
50
50

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

C Program to add elements of an array

How to add elements in an array using C Program. Add elements of an Array …

Leave a Reply

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