16/02/2019

Find Factorial of a number by C-Programming(Using While-loop)

Find Factorial of a number by C-Programming(Using While-loop):

So the definition of factorial is if we want to calculate the factorial value of any number n then we have to multiply all the natural number less than n with n i.e in easy way we have to calculate 1*2*3*...*(n-1)*n. so we first give the programming code for this programming the explain how it works.
Find Factorial of a number by C-Programming(Using While-loop)
Factorial of a number

The most important things that you have to know to understand the program:
1. the uses of int, printf, scanf.
2. while loop.

Find Factorial of a number by C-Programming(Using While-loop)

#include<stdio.h>
int main()
{
int n,i,fact=1;
printf("Enter the number:");
scanf("%d",&n);
i=1;
while(i<=n)
    {
        fact=fact*i;
        i++;
    }
    printf("The factorial of %d is %d",n,fact);
}

 The output is here below:

Find Factorial of a number by C-Programming(Using While-loop) output

Explanation:

1. First of all we introduce package <stdio.h>.
2. we introduce three integer variable. we use n for taking value from users i.e. which number you like to have factorial. we use i for while loop and we use fact to calculate the factorial and give the value as a output.
3. we initialize i and fact with the value 1 to avoid the garbage value. and here i++ is stand for increase the value of i by 1 after every step of while loop.
4. we use while loop to solve the program you can also use if-else or for loop to solve it.
5. Here is some limitation, 1st one is you can get correct value till 12! after that it will give some garbage value. so for increase the number you have to use long command before int. After that you can get the correct value of 13!,14! and so on. It has also have limit. For more information about long visit our 'syntax page'

No comments:

Post a Comment