Name: Arpan Bhowmick
Roll No: CSC/23/5
// name: Arpan Bhowmick
// Roll no: CSC/23/5
#include <iostream>
using namespace std;
// Function to find factorial of a number using recursion
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
// Function to find power of a number using recursion
int power(int base, int exponent) {
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}
// Function to find sum of first n natural numbers using recursion
int sumOfNaturalNumbers(int n) {
if (n == 0)
return 0;
else
return n + sumOfNaturalNumbers(n - 1);
}
// Function to find fibonacci series up to n terms using recursion
void fibonacci(int n, int a = 0, int b = 1) {
if (n > 0) {
cout << a << " ";
fibonacci(n - 1, b, a + b);
}
}
int main() {
int choice, num, exponent;
do {
cout << "Choose an operation:\n";
cout << "1. Factorial of a number\n";
cout << "2. Power of a number\n";
cout << "3. Sum of first n natural numbers\n";
cout << "4. Fibonacci series upto n terms\n";
cout << "5. Exit\n";
Name: Arpan Bhowmick
Roll No: CSC/23/5
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter a number: ";
cin >> num;
cout << "Factorial of " << num << " is: " << factorial(num) <<
endl;
break;
case 2:
cout << "Enter the base: ";
cin >> num;
cout << "Enter the exponent: ";
cin >> exponent;
cout << num << " raised to the power " << exponent << " is: "
<< power(num, exponent) << endl;
break;
case 3:
cout << "Enter the value of n: ";
cin >> num;
cout << "Sum of first " << num << " natural numbers is: " <<
sumOfNaturalNumbers(num) << endl;
break;
case 4:
cout << "Enter the number of terms: ";
cin >> num;
cout << "Fibonacci series upto " << num << " terms is: ";
fibonacci(num);
cout << endl;
break;
case 5:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice! Please try again.\n";
break;
}
} while (choice != 5);
return 0;
}
Name: Arpan Bhowmick
Roll No: CSC/23/5
Output
Name: Arpan Bhowmick
Roll No: CSC/23/5