C Program to Find Factorial of a Number

C program to find factorial of a number; Through this tutorial, we will learn how to find factorial of a number in c program using for loop, while loop, recursion and Function.

Algorithm of C Program for Factorial

Use the algorithm to write a program to find factorial of a number; as follows:

  • Start program
  • Ask the user to enter an integer to find the factorial
  • Read the integer and assign it to a variable
  • From the value of the integer up to 1, multiply each digit and update the final value
  • The final value at the end of all the multiplication till 1 is the factorial
  • End program

Programs to Find Factorial of a Number in C

  • C Program to find Factorial of a Number Using While Loop
  • C Program to find Factorial of a Number Using For Loop
  • C Program to find Factorial of a Number Using Recursion
  • C Program to find Factorial of a Number Using Function

C Program to find Factorial of a Number Using While Loop

#include<stdio.h>

int main(){

    int x=1,fact=1,n;

    printf("Enter a number to find factorial: ");

    scanf("%d",&n);

    while(x<=n){

        fact=fact*x;

        x++;

    }

    printf("Factorial of %d is: %d",n,fact);

    return 0;

}

The output of the above c program; as follows:

Enter a number to find factorial: 5
Factorial of 5 is: 120

C Program to find Factorial of a Number Using For Loop

#include<stdio.h>

int main(){

    int x,fact=1,n;

    printf("Enter a number to find factorial: ");

    scanf("%d",&n);

    for(x=1;x<=n;x++)

        fact=fact*x;

    printf("Factorial of %d is: %d",n,fact);

    return 0;

}

The output of the above c program; as follows:

Enter a number to find factorial: 6
Factorial of 6 is: 720

C Program to find Factorial of a Number Using Recursion

#include <stdio.h>

int main(){

int n;

printf("Enter a number to find factorial: ");

scanf("%d",&n);

printf("The factorial of the number is %d", fact(n));

return 0;

}

// Recursive function to find factorial

int fact(int y){

if (y == 0)

return 1;

return y * fact(y - 1);

}

The output of the above c program; as follows:

Enter a number to find factorial: 9
The factorial of the number is 362880

C Program to find Factorial of a Number Using Pointer

#include <stdio.h>

int fact(int);

void main()
{
 int no,factorial;

  	printf("Enter a number to calculate it's factorial :- ");
  	scanf("%d",&no);
  	factorial=fact(no);
    printf("Factorial of the num(%d) = %d\n",no,factorial);
//printf("Factorial of the num(%d) = %d\n",no,fact(no));//another way of calling a function//comment above two lines if you want to use this
}

int fact(int n)
{
    int i,f=1;
    for(i=1;i<=n;i++)
    {
        f=f*i;
    }
    return f;
}

The output of the above c program; as follows:

Enter a number to calculate it's factorial :- 8
Factorial of the num(8) = 40320

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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