Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int computeGCD(int a, int b);
- int computeGCDRecursive(int a, int b);
- double computeFactorial(int n);
- double computeFactorialRecursive(int n);
- int main() {
- //GCD = > Greatest Common Divisor
- cout << computeGCD(21, 49);
- return 0;
- }
- int computeGCD(int a, int b) {
- while (b != 0) {
- int temp = b;
- b = a % b;
- a = temp;
- }
- return a;
- }
- int computeGCDRecursive(int a, int b) {
- //1. Conditia de oprire
- if (b == 0) {
- return a;
- }
- //2 Apelul recursive
- return computeGCDRecursive(b, a % b);
- }
- // Compute factorial: n! => 1 * 2 * 3 *.. *n => 4! => 4 * 3!
- double computeFactorial(int n) {
- double result = 1;
- for (int i = 1; i <=n;i++) {
- result = result * i;
- }
- return result;
- }
- double computeFactorialRecursive(int n) {
- if (n <= 1) {
- return 1;
- }
- return n * computeFactorialRecursive(n-1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement