![]() ![]() ![]() In above table gcd(33, 0) gets called, since n2 = 0, our program returns value of n1 as gcd, which is 33. Lets assume that user has entered n1 = 1980 and n2 = 1617 n1 Printf("\nGCD of %d and %d is %d.\n", num1, num2, gcd(num1, num2)) Printf("Enter 2 positive integer numbers\n") Source Code: C Program To Find GCD of Two Numbers using Recursion: Euclid’s Algorithm #include Video Tutorial: C Program To Find GCD of Two Numbers using Recursion: Euclid’s Algorithm If n2 is 0, then value present in n1 is the gcd of (n1,n2). If n1 is 0, then value present in n2 is the gcd of (n1,n2). We need to recursively execute above 2 lines of logic until either n1 is 0 or until n2 is 0. If n2 > n1, we need to pass gcd(n1, n2%n1) If n1 > n2 we need to pass gcd(n1%n2, n2) Ex: gcd(n1, n2) Īccording to Euclid’s Algorithm, we’ll get the same gcd if we reduce the bigger number by modulo dividing it by smaller number. For example, if n1 is greater than n2, then reduce the value of n1 by replacing it with n1%n2.Īssume that we’ve a function gcd() which returns gcd of 2 numbers passed to it. If user inputs 2 numbers n1 and n2, reduce the bigger number by modulo dividing it by the smaller number. ![]() Source Code: C Program To Find GCD of Two Numbers using Recursion and Ternary or Conditional Operator: Euclid’s Algorithm. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |