0% found this document useful (0 votes)
5 views7 pages

Assignment DSA

The document contains a series of programming tasks focused on recursive functions in C++. It includes implementations for calculating power, multiplication, factorial, summing array elements, and reversing an array using recursion. Each task is accompanied by example code and prompts for user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Assignment DSA

The document contains a series of programming tasks focused on recursive functions in C++. It includes implementations for calculating power, multiplication, factorial, summing array elements, and reversing an array using recursion. Each task is accompanied by example code and prompts for user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME: WAQAR ALI

ROLL NO: BSIT51F21R051RE


CLASS: BSIT 6TH (Reg)
Subject: Retake DSA
Submitted To: Mam Anaam Sahar
Q-1: Write a recursive function power() to calculate the power of a
number (a^b) without using the pow() function. Take two integers
as input (base and exponent) and display the result.
#include <iostream>
using namespace std;

int power(int base, int exp) {


if (exp == 0) return 1;
// Base case
return base * power(base, exp - 1);
}

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 multiply(int a, int b) {


if (b == 0) return 0;
return a + multiply(a, b - 1);
}

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 getSum(int arr[], int size) {


if (size == 0) return 0;
return arr[size - 1] + getSum(arr, size - 1); }

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];

cout << "Sum: " << getSum(arr, size) << endl;


return 0;
}
Q-5: Write a recursive function reverseArray() to reverse an array.
Take user input for the array size and elements, then display the
reversed array using recursion.
#include <iostream>
using namespace std;

void reverseArray(int arr[], int start, int end) {


if (start >= end) return; // Base case
swap(arr[start], arr[end]);
reverseArray(arr, start + 1, end - 1); // Recursive step
}

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];

reverseArray(arr, 0, size - 1);

cout << "Reversed array: ";


for (int i = 0; i < size; i++) cout << arr[i] << " ";
cout << endl;
return 0;
}

You might also like