Showing posts with label factorial. Show all posts
Showing posts with label factorial. Show all posts

Monday, 16 October 2017

Factorial in Python

num=int(input("Enter an integer to find factorial : "))
fact=1
if num<0:
        print("Factorial of negative numbers does not exist.")
elif num==0:
        print("Factorial of 0 is 1.")
elif num==1:
        print("Factorial of 1 is 1.")
else:
        for i in range(1,num+1):
                fact=fact*i
        print("Factorial of ",num," is ",fact)

Output


Factorial using Recursion in python

def fact_recurse(n):
        if n==1:
                return n
        else:
                return n*fact_recurse(n-1)
num=int(input("Enter an integer to find factorial : "))
if num<0:
        print("Factorial of negative numbers does not exist.")
elif num==0:
        print("Factorial of 0 is 1.")
else:
        print("Factorial of ",num," is ",fact_recurse(num))

Output


Sunday, 15 October 2017

Factorial in c++

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
        int num, fact=1;
        cout<<"Enter a number to find factorial : ";
        cin>>num;
        for(int i=1;i<=num;i++)
        {                 fact=fact*i;
        }
        cout<<"Factorial of "<<num<<" is "<<fact;
        getch();
        return 0;
}

Output


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]         ...