Is there any pre defined function to find the HCF of more than two numbers in c++? | Sololearn: Learn to code for FREE!

+2

Is there any pre defined function to find the HCF of more than two numbers in c++?

We can find the HCF of two numbers by gcd function,But is it possible in c++ to find the HCF of more than two numbers (Like HCF of 1,2,3) WITH PRE-DEFINED FUNCTION?

c++

5/14/2022 4:30:47 PM

Manav Roy

8 Answers

New Answer

+1

I would say you don't need another function. Theoretically this should work: gcd(1, gcd(2,3))

+1

Sure :) You just need to convince yourself of the recursive step gcd(ds(1..n)) = gcd(d(1), gcd(ds(2..n)) for a sequence of positive integers d(1), ..., d(n), where by ds(i..j) I mean d(i), ..., d(j). This is a recursion over the length of the sequence. The base case being gcd(d) = d

+1

Ani Jona πŸ•Š Kind of complicated, What I've thinking of is that, Suppose if I've following numbers- 2,4,10,72,42 So,If i do find hcf of 42 and 72 , Which is 6, Then HCF of 6 and 10 , Which is 2. Then HCF of 2 and 4 , Which is 2, Then HCF of 2 and 2, Which is 2. Though it's working alright,But , Would it be applicable for all cases?

0

I_MATODOR_I What about 2,16,56,42,100,28,98,42 hcf?

0

Use an array for all numbers. Use a for loop. https://www.tutorialspoint.com/cplusplus-program-for-gcd-of-more-than-two-or-array-numbers

0

Wonderful task to practise recursion :)

0

Ani Jona πŸ•Š Oh, Could it be done via recursions?

0

Yes, it would :)