program of factorial in c | C programming by TechWithCode

 program of factorial in c

Hello and Welcome to www.TechWithCode.com


Today we will learn about how to write a C Program to find the factorial of a number. Before moving forward, we must have knowledge about what factorial is?

What is Factorial?

Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:

5! = 5*4*3*2*1 = 120  

3! = 3*2*1 = 6  

Summary: Factorial is represented using '!', so five factorial will be written as (5!),n factorial as (n!).

n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.

There can be many ways in programming to find the factorial of any number but today we will discuss two important way with the source code

  1. Factorial Program using loop
  2. Factorial Program using recursion

1. Factorial Program using loop

Problem Statement⤵️

Write a C program that calculates the factorial of any number, this number should be taken as input from the keyboard.

Source code⤵️


#include <stdio.h>

int main()
{
	int c, n, fact = 1;
	
	printf("Enter a number to calculate it's factorial\n");
	scanf("%d", &n);
	
	for (c = 1; c <= n; c++)
		fact = fact * c;
	
	printf("Factorial of %d = %d\n", n, fact);
	
	return 0;
}

output⤵️

factorial,factorial program,c program,factorial program in c,factorial of a number,c programming,factorial program in c++,factorial in c,factorial number program in c,c program for factorial of a number,c program for finding factorial in hindi,factorial of a number in c,factorial of a given number in c,c++ factorial program,c program for factorial,factorial of a number in c using for loop,factorial of 0,factorial of a number program in c,program on factorial in c


2. Factorial Program using recursion

Problem Statement⤵️

Write a C program that calculates the factorial of any number using recursion, this number should be taken as input from the keyboard.

Source code⤵️


#include<stdio.h>
int fact(int);
int main(){
	int num,f;

	printf("\nEnter a number: ");
	scanf("%d",&num);

	f=fact(num);

	printf("\nFactorial of %d is: %d",num,f);
	return 0;
}

//recursive function
int fact(int n){
	if(n==1)
		return 1;
	else
		return(n*fact(n-1));
}

output⤵️

factorial,factorial program,c program,factorial program in c,factorial of a number,c programming,factorial program in c++,factorial in c,factorial number program in c,c program for factorial of a number,c program for finding factorial in hindi,factorial of a number in c,factorial of a given number in c,c++ factorial program,c program for factorial,factorial of a number in c using for loop,factorial of 0,factorial of a number program in c,program on factorial in c


No comments:

Do not add any link in the comments.
For backlink, contact us.

Powered by Blogger.