Write a Program to find factorial of a integer number N

InputHow factorial works by using recursion
5

factorial(5)

=5*factorial(5-1)

=5*factorial(4)

=5*{4*factorial(4-1)}

=5*4*factorial(3)

=5*4*{3*factorial(3-1)}

=5*4*3*factorial(2)

=5*4*3*{2*factorial(2-1)}

=5*4*3*2*factorial(1)

=5*4*3*2*{1*factorial(1-1)}

=5*4*3*2*1*factorial(0)

=5*4*3*2*1*{1}

=5*4*3*2*{1*1}

=5*4*3*{2*1}

=5*4*{3*2}

=5*{4*6}

=5*24

=120

Source Code:

#include<bits/stdc++.h>

unsigned long long factorial(unsigned long long);

int main()

{

    unsigned long long p,result;

    while(scanf(“%llu”,&p)==1)

    {

        result=factorial(p);

        printf(“Factorial: %llu\n”,result);

    }

    return 0;

}

unsigned long long factorial(unsigned long long n)

{

    if(n==0)

        return 1;

    else

        return n*factorial(n-1);

}

Share

2 thoughts on “Write a Program to find factorial of a integer number N

  1. F*ckin’ amazing things here. I’m very glad to see your post. Thanks a lot and i’m looking forward to contact you. Will you please drop me a e-mail?

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Lean Blog by Crimson Themes.