Write a program to calculate factorial and to compute the factors of a given no. (i) using recursion, (i) using iteration

by

Last updated on Nov 19, 2022
DS Practicals (Question 11)

Input

#include<iostream>
using namespace std;
int i = 1;
int factorial(int n);
void factor(int n, int i);
int iter_factorial(int n);
void iter_factor(int n);

int main()
{
    cout << "\n\t ~~~~~~~~~~~~~~~~~~~~~~~Practical 11~~~~~~~~~~~~~~~~~~~~\n\t\t\t\t  \n";
    int n;

    cout << "Enter a positive integer: ";
    cin >> n;

    cout << "Factorial of " << n << " using recursion = " << factorial(n);
    cout<<endl;
    cout << "Factors of " << n << " using recursion = " ;
    factor(n,i);
    cout<<endl;
    cout << "Factorial of " << n << " using iteration = " << iter_factorial(n);
    cout<<endl;
    cout << "Factors of " << n << " using iteration = " ;
    iter_factor(n);
    cout<<endl;

    return 0;
}

void factor(int n,int i)
{
    if (i<= n) {
        if (n % i == 0) {
            cout << i << " ";
        }
 
        factor(n, i + 1 );
    }
}
 

int factorial(int n)
{
    if(n > 1)
        return n * factorial(n - 1);
    else
        return 1;
}

int iter_factorial(int n){
    int fact = 1;
    for(int i=1;i<=n;i++){
        fact = fact *i;
    }
    return fact;
}

void iter_factor(int n){
    
    for(int j = 1; j <= n; j++) {
        if(n % j == 0)
            cout << j << " ";
    }
    
    

}

How useful was this post?

5 star mean very useful & 1 star means not useful at all.

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Tags: