Program to find GCD of two numbers using recursion


Default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Write a program to find gcd/hcf of given two numbers using recursion
//Eg: GCD(18,24)=6 , GCD(23,9)=1

 
#include <stdio.h>
int gcd(int ,int );
int main()
{
    int n1,n2;
    printf("Enter first number:");
    scanf("%d",&n1);
    printf("Enter second number:");
    scanf("%d",&n2);
    if(n1<=0 || n2<=0)
        printf("Invalid input...!\n");
    else
        printf("GCD of given numbers=%d\n",gcd(n1,n2));
    return 0;
}
int gcd(int n1,int n2)
{
    if(n1%n2==0)
        return n2;
    else
        return gcd(n2,n1%n2);
}

No comments:

Post a Comment