Header Ads Widget

Develop Your Creative Skill with
Free Design Tutorials

Our blog helps to Provide Latest Tips and Tricks tutorials about Blogging
Business, SEO Tips, much more. this Blog and stay tuned to this
blog for further updates.

print factorial of a number in C

Program to Print Factorial of a Number in C

 There are lots of ways to print factorial of a number in C programming languages. I have listed down some common methods to print factorial of a number in C.

Ways to Print factorial of a Number

  1. Factorial of Number using for Loop.
  2. Factorial of a Number using While loop.
  3. Factorial of a Number using do While loop.
  4. Factorial of a Number using Recursion.
  5. Factorial of a Number using Function call.


What is the Factorial of a Number in C?

Factorial of a number (N) is defined as the product of all numbers from 1 to N.Logic used in all the methods to print factorial of a number is that we will input a number from the user and then multiplying all numbers by decreasing/increasing it until the loop reaches 1 or N. Above code is for only Positive numbers You can add an If else statement to check whether a number is positive or negative. If the number is negative then Output should be " Negative number's Factorial doesn't exist.


5 ways to print factorial of a number in C


Find Factorial of a number using For loop in C


//@Courpedia
#include<stdio.h>
void main()
{
int i,factorial=1,n;
printf("enter the number\n");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
    factorial=factorial*i;
}
printf("%d"factorial);
}


Another way to find the factorial of a number using c 


//@Courpedia
#include<stdio.h>
void main()
{
int i,n,fact=1;
printf("enter the number\n");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
    fact=fact*i;
}
printf("%d"fact);
}



Find Factorial of a number using While loop in C


//@Courpedia
#include<stdio.h>
void main()
{
int i,n,fact=1;
printf("enter the number\n");
scanf("%d",&n);
int temp=1;
while(temp<=n)
{
    fact=temp*fact;
    temp++;
}
printf("%d"fact);
}



Find Factorial of a number using Do While loop in C



//@Courpedia
#include<stdio.h>
void main()
{
int i,n,fact=1;
printf("enter the number\n");
scanf("%d",&n);
int temp=1;
do{
    fact=fact*temp;
    temp++;

}while(temp<=n);
printf("%d"fact);
}


Find Factorial of a Number using Recursion in C

Recursion means accessing the function itself in the same function.


//@courpedia
#include <stdio.h>
unsigned long fact(unsigned long int n)
{
    if (n == 0)
    {
        return 1;
    }
    else
    {
        return(n * fact(n - 1));
    }
}
int main(int argc, char *argv[])
{
    unsigned long  n = 0;
    unsigned temp = 0;
    printf("Enter the number ");
    scanf("%lu"&n);
        temp = fact(n);
        printf("  %d \n"temp);
    
    return 0;
}





Also Read -  C programming interview questions


Find Factorial of a number using Function in C


//courpedia
#include<stdio.h>
int fact(int);
void main()
{
    int n;
    printf("enter the number\n");
    scanf("%d",&n);
    int b=  fact(n);
    printf("%d"b);
    
}
int fact(int n)
{
    int result=1;
for(int i=1;i<=n;i++)
{
    result=result*i;
}
return result;

}



You can try your own logic to find factorial of a number In C Programming Langauge in various other ways.

Learn Various Languages with Us Explore our various other Informative posts.