C Program to find GCD of number using Recursive Function





According to Wikipedia : In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.
Similarly, The number 54 can be expressed as a product of two integers in several different ways:

 54 \times 1 = 27 \times 2 = 18 \times 3 = 9 \times 6. \,
Thus the divisors of 54 are:
 1, 2, 3, 6, 9, 18, 27, 54. \,

 
 
So now let us tell our computer to do this whole work, by using following program you can find GCD of two numbers
 
Program:

//program to find out the GCD of the entered number, Program by Slashmycode.com

#include <stdio.h>

int hcf(int n1, int n2);

int main()

{

   int n1, n2;

   printf("Enter two positive integers: ");

   scanf("%d%d", &n1, &n2);

   printf("H.C.F of %d and %d = %d", n1, n2, hcf(n1,n2));

   return 0;

} //program to find out the GCD of the entered number, Program by Slashmycode.com

int hcf(int n1, int n2)

{

    if (n2!=0)

       return hcf(n2, n1%n2);

//program to find out the GCD of the entered number, Program by Slashmycode.com

    else

       return n1;

}


Output:
 
output for C Program to find GCD of number using Recursive Function.
 

No comments:

Post a Comment

Feel free to ask us any question regarding this post