write a program to find factorial of a number code example

Example 1: factorial of a given number in c

//Factorial of a given number
#include <stdio.h>

//This function returns factorial of the number passed to it 
long int factorialOf(int number){
    long int factorial = 1;
    while(number){
        factorial*=number;
        number-=1;
    }
    return factorial;
}

int main(void) {
	int n;
	printf("Find factorial of \n");
	scanf("%d",&n);
	printf("\nThe factorial of %d is %ld",n,factorialOf(n));
	return 0;
}

Example 2: write a program to find factorial of a number

#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;

Tags:

C Example