bsc

Comprehensive codebase and coursework for the 4-year B.Sc. Computer Science Honours degree (WBSU)

git clone https://github.com/notamitgamer/bsc.git
· rebuilt hourlyOpen on GitHub
root / semester_2 / assignments / assignment_14.cpp

assignment_14.cpp (2335B)


      1 /* WAP to calculate factorial and to compute the factors of a given no. (i) using recursion, (ii) using iteration. */
      2 
      3 #include <iostream>
      4 using namespace std;
      5 
      6 class MathOperations {
      7 private:
      8     int num;
      9 
     10     long long factorialRecursive(int n) {
     11         if (n <= 1) return 1;
     12         return n * factorialRecursive(n - 1);
     13     }
     14 
     15     void factorsRecursive(int n, int divisor) {
     16         if (divisor > n) return;
     17         if (n % divisor == 0) cout << divisor << " ";
     18         factorsRecursive(n, divisor + 1);
     19     }
     20 
     21 public:
     22     MathOperations(int n) { num = n; }
     23 
     24     void calculateFactorialRecursive() {
     25         if (num < 0) { cout << "Not defined for negative numbers.\n"; return; }
     26         cout << "Factorial (Recursive): " << factorialRecursive(num) << endl;
     27     }
     28 
     29     void calculateFactorialIterative() {
     30         if (num < 0) { cout << "Not defined for negative numbers.\n"; return; }
     31         long long fact = 1;
     32         for (int i = 2; i <= num; i++) fact *= i;
     33         cout << "Factorial (Iterative): " << fact << endl;
     34     }
     35 
     36     void computeFactorsRecursive() {
     37         if (num <= 0) { cout << "Factors need a positive integer.\n"; return; }
     38         cout << "Factors (Recursive): ";
     39         factorsRecursive(num, 1);
     40         cout << endl;
     41     }
     42 
     43     void computeFactorsIterative() {
     44         if (num <= 0) { cout << "Factors need a positive integer.\n"; return; }
     45         cout << "Factors (Iterative): ";
     46         for (int i = 1; i <= num; i++) if (num % i == 0) cout << i << " ";
     47         cout << endl;
     48     }
     49 };
     50 
     51 int main() {
     52     int n, choice;
     53     cout << "Enter a number: ";
     54     cin >> n;
     55     MathOperations op(n);
     56 
     57     while (true) {
     58         cout << "1.Factorial(Recursive) 2.Factorial(Iterative) "
     59              << "\n3.Factors(Recursive) 4.Factors(Iterative) 0.Change Number -1.Exit\n";
     60         cout << "Enter your choice: ";
     61         cin >> choice;
     62 
     63         switch (choice) {
     64             case 1: op.calculateFactorialRecursive(); break;
     65             case 2: op.calculateFactorialIterative(); break;
     66             case 3: op.computeFactorsRecursive(); break;
     67             case 4: op.computeFactorsIterative(); break;
     68             case 0: cout << "Enter new number: "; cin >> n; op = MathOperations(n); break;
     69             case -1: cout << "Exiting...\n"; return 0;
     70             default: cout << "Wrong choice.\n";
     71         }
     72     }
     73 }