Monday 16 October 2017

Factorial using recursion in c++

#include<iostream>
#include<conio.h>
using namespace std;
int fact_recurse(int n)
{
        if(n==1)
        return n;
        else
        return n*fact_recurse(n-1);
}
int main()
{
        int num;
        cout<<"Enter an integer to find factorial : ";
        cin>>num;
        if(num<0)
        cout<<"Factorial of negative numbers does not exist.";
        else if(num==0)         cout<<"factorial of 0 is 1.";
        else
        cout<<"Factorial of "<<num<<" is "<<fact_recurse(num);
        getch();
        return 0;
}

Output


No comments:

Post a Comment

Python Program to find Fabonacci

fabtab={} def fabonacci(n):     fabtab[0]=0     fabtab[1]=1     for i in range(2,n+1):         fabtab[i]=fabtab[i-1]+fabtab[i-2]         ...