C++ Program to print factorials of a given range

C++ Program to print factorials of a given range

        In this program you need to print all the factorials of numbers you encounter in a specific given range. The logic is same as of the normal factorial program that is multiplying all the numbers from till current number. Only one loop will be added to the same logic to traverse through the range.

Code

#include<bits/stdc++.h>
using namespace std;


int fact(long long int x)
{
    long long int fact=1;
    
    for(int i=x;i>0;i--){
        fact=fact*i;
    }
    return fact;
}


int main()
{
    cout<<
"Enter a range"<<endl;
    int m,n;
    cin>>m>>n;
    
    cout<<
"Factorial are: "<<endl;

    for(int i=m;i<=n;i++){
    cout<<fact(i)<<endl;
    }


    return 0;
}


Input:

Enter a range

1 5

Output:

Factorial are: 
1
2
6
24
120


Post a Comment

0 Comments