Find the Factorial of the numbers

 

Here is an example of finding the factorial of any number in the c programming language.

code:

//how to find the factorials of the numbers


#include<stdio.h>

int main()

{

int n = 0;        //variable declare to store the value of user 

int fact = 1;        //variable declare to calculate factorials

printf("Calculating the factorial of the number entered by the user.\n");

printf("Enter the number:");

scanf("%d", &n);

while (n)

{

fact = fact*n;

n--;

}

printf("\nfact of given number is  %d.\n", fact);

getchar;

return 0;

}


In the above code, two int variables fact and n.n is used to store the number of the user wants to find the factorial, which is used to store the value of the factorial.

After taking input from the user by the input method, If you do not about the input method visit how to input.

While loop used, in the body of the loop multiple the counter n with fact and store in the same variable. The loop is executed till n is not equal to zero.

At the end display the stored value of the fact which is the factorial of the given number.

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout