C program to find the factorial of a number - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Wednesday, January 8, 2020

C program to find the factorial of a number - My CS Tutorial

C program to find the factorial of a given number.
In this tutorial we will learn how to write a c program to find the factorial of a given number.
If we will enter a number as 5 then the factorial of 5 is:-

5 * 4 * 3 * 2 * 1 = 120

The factorial of 5 is 120.

Example-




C program code


#include <stdio.h>
#include<conio.h>

long int fact(int n);

int main()

{

    int n;

    printf("Enter a positive integer: ");

    scanf("%d", &n);

    printf("\nFactorial of %d = %ld", n, fact(n));

    return 0;

}

long int fact(int n)

{

    if (n >= 1)

        return n*fact(n-1);

    else

        return 1;

}


OUTPUT:






Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment