Header Ad

C Program to Print Factorial of any given number

In this tutorial, we are going to make a C Program to print a Factorial of any given number entered by the user.

C Program to Print Factorial of any given number

What is a Factorial of a Number?

The factorial of a number is equal to the multiplication of the number's factors. let's say we want to get the factorial of number 4. so the factorial of number 5 is 1*2*3*4*5 =  120. now let's make a c program to print the factorial of a given number.


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

void main()
{

           int input, i, fact=1;

           printf("Enter any number: ");
           scanf("%d", &input);

           for( i=1; i<=input; i++)
               fact= fact*i;

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

         getch();
}

Output

C Program to Print Factorial of any given number

Explanation

Here first we include the two header files stdio.h and conio.h. after that we declare the main function. and inside that, we declare the three variables input, i, and fact, and set the value of fact variable to 1. after that we print the message on the screen "Enter any number:" using printf() function. and then we scan the value that given or enter by the user in the input variable using the scanf() function.

after that, we use the for loop to find the factorial of the given number. remember the for loop will increase the value of i and multiply with the fact variable. and the loop will run till the value of i will not equal the input variable.

now using the printf() function we print the factorial and the message on the output screen.

Post a Comment

0 Comments