Program to calculate Factorial of a given number

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int num, fact = 1;
    
    /*Accept a number from the user*/
    printf("\nEnter the number: ");
    scanf("%d", &num);
    
    /*Calculate the factorial*/
    for(int i=1; i<=num; i++)
    {
    	fact = fact * i;
    }
    
    /*Display the result*/
    printf("The factorial of %d is %d", num, fact);
    
    return 0;
}

Output:

Enter the number: 5
The factorial of 5 is 120

You may also like...

4 Responses

  1. Jyant Khosla says:

    void main(){
    int n,s=1;
    printf(“enter the nth value”);
    scanf(“%d”,&n);
    while(n>0)
    {
    s=s*n;
    n–;
    }
    printf(“the answer is %d”,s);
    getch();
    }

  2. shubha says:

    i get ans fr factorial oly up to 10

  1. March 1, 2014

    […] Progarm to calculate Factorial of a given number […]

  2. October 25, 2017

    […] Progarm to calculate Factorial of a given number […]

Leave a Reply

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

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet