Docsity

C++ Recursive Functions: Sum, Max, Palindrome, Reverse, Power, GCD, Fibonacci, Assignments of Object Oriented Programming

Partial preview of the text

Download C++ Recursive Functions: Sum, Max, Palindrome, Reverse, Power, GCD, Fibonacci and more Assignments Object Oriented Programming in PDF only on Docsity!

#include using namespace std; int SumOfArray(int n[], int size, int sum) { if (size == -1) { return sum; } else { sum = n[size] + sum; size--; SumOfArray(n, size, sum); } } int main() { int num[10]; cout << "enter 10 elements of array:" << endl; for (int i = 0; i<10; i++) { cin >> num[i]; } int sum = SumOfArray(num, 9, 0); cout << "sum of array: " << sum << endl; system("pause"); return 0; }

Task 5

#include #include <math.h> using namespace std; int MaxElement(int n[], int size, int max) { if (size == -1) { return max; } else { if (n[size]>max) { max = n[size]; } size--; MaxElement(n, size, max); } } int main() { int num[10]; cout << "enter array" << endl; for (int i = 0; i<10; i++) { cin >> num[i]; } int max = MaxElement(num, 9, 0); cout << "max element of array: " << max << endl; system("pause"); return 0; }

Output

#include #include using namespace std; void Backward(string str, int length) { if (length<0) { return; } else { cout << str[length]<<endl; length--; Backward(str, length); } } int main() { string str; cout << "enter a string " << endl; cin >> str; Backward(str, str.length() - 1); system("pause"); return 0; }

Output

#include #include using namespace std; void ReverseDigit(int n) { if (n == 0) { return; } else { int mod = n % 10; n = n / 10; cout << mod<<endl; ReverseDigit(n); } } int main() { int number; cout << "enter a number" << endl; cin >> number; ReverseDigit(number); system("pause"); return 0; }

#include using namespace std; int gcd(int n1, int n2) { if (n2 == 0) return n1; if (n2 != 0) return gcd(n2, n1 % n2); } int main() { int n1, n2; cout << "Enter two positive integers:" << endl; cin >> n1; cin >> n2; cout << "GCD of " << n1 << " and " << n2 << " = " << gcd(n1, n2); system("pause"); return 0; }

Task 11

#include using namespace std; int A(int m, int n); int main() { int m, n; cout<<"Enter two numbers \n"; cin>>m; cin >> n; cout << "\nOUTPUT: "; cout<< A(m, n); cout << endl; system("pause"); } int A(int m, int n) { if (m == 0) return n + 1; else if (n == 0) return A(m - 1, 1); else return A(m - 1, A(m, n - 1)); }

#include #include <math.h> using namespace std; void FibonacciNthNum(int a, int b, int sum, int n) { if (n == 2) { return; } else { sum = a + b; cout << sum << " "; a = b; b = sum; FibonacciNthNum(a, b, sum, n - 1); } } int main() { int a = 0, b = 1, n; cout << "enter nth number:"; cin >> n; cout << a << " " << b << " "; FibonacciNthNum(a, b, 2, n); system("pause"); return 0; }

#include #include<math.h> using namespace std; void BinaryToDecimal(int binary, int decimal, int rem, int p) { if (binary == 0) { cout << "decimal form of binary enetered is " << decimal << endl; return; } else { rem = binary % 10; decimal = decimal + (rem * pow(2, p)); p++; binary = binary / 10; BinaryToDecimal(binary, decimal, rem, p); } } int main() { int binary; cout << "enter a binary number" << endl; cin >> binary; BinaryToDecimal(binary, 0, 0, 0); system("pause"); return 0; }