Assignment DSA
Assignment DSA
int main() {
int base, exp;
cout << "Enter base and exponent: ";
cin >> base >> exp;
cout << "Result: " << power(base, exp) << endl;
return 0;
}
Q-2: Write a recursive function mult() to multiply two numbers without using
the * operator. Call this function with two numbers and display the result.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Product: " << multiply(a, b) << endl;
return 0;
}
Q-3: Implement a recursive function factorial() that computes the
factorial of a given number. Call this function with an example
input and display the result.
#include <iostream>
using namespace std;
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factorial: " << factorial(num) << endl;
return 0;
}
Q-4: Write a recursive function getSum() to compute the sum of
elements in an array. Take user input for the array size and
elements, then display the sum.
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter array size: ";
cin >> size;
int arr[size];
cout << "Enter elements: ";
for (int i = 0; i < size; i++) cin >> arr[i];
int main() {
int size;
cout << "Enter array size: ";
cin >> size;
int arr[size];
cout << "Enter elements: ";
for (int i = 0; i < size; i++) cin >> arr[i];