60 564
Assignments Done
97,9%
Successfully Done
In April 2018

Answer to Question #59973 in C++ for adrain

Question #59973
Write two recursive integer
functions. The first function should calculate factorials
using the definition 0! = 1 and n! = (n-1)! x n. In
addition, if n is too large it should be able to detect
integer overflow before it happens. The next function
should calculate the greatest common factor (gcf) of two
numbers. If the second number is zero, then the gcf is
the other number. Otherwise, the gcf of a and b is the
same as the gcf of b and a mod b.
Expert's answer
int factorial(int num) {
if(num > 12) throw;
if(num <= 0) return 1;
return factorial(num - 1) * num;
}

int gcf(int a, int b) {
if(b > a) {
int tmp = b;
b = a;
a = tmp;
}

if(b == 0) return a;
return gcf(b, a % b);

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions