C Program – Find GCD of two numbers

By | 2017-09-08T15:38:51+00:00 September 8th, 2017|c-program|0 Comments

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; 
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

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

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

[/box]

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); 
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

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

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

[/box]

Happy Learning 🙂

About the Author:

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+ ,

Leave A Comment