Calculate the factorial n!

Threads: Write a program to calculate n! với n là số tự nhiên không âm nhập vào từ bàn phím

Để làm được bài này, First you need to recall the formula n! skin. By definition we have factorial:

  • 0! = 1
  • n! = 1.2.3…n

Vậy là ta có công thức rồi. Looking at the formula we see with n = 0 it's easy, if n> 0 thì nó là tích các số từ 1 to n. Vậy chúng ta có thể dùng loop để tính dễ dàng 🙂

/*
* Calculate n!
*/

#include <stdio.h>

int main()
{
	int i, n;
	int fact = 1;

	printf("Enter n = ");
	scanf("%d", &n);

	for(i = 1; i <= n; i++) 
	{
		fact = fact * i;
	}

	printf("%d! = %d\n", n, fact);

	return 0;
}

Khá dễ dàng. However, lest you notice a little confused.

  • Why do we assign initial values ​​for variables fact (Factorial) was 1 which is not 0 or some other? Simply because if assigned fact = 0 the fact always 0 do 0 multiplied by the number is 0.
  • Why do not we review the code case n = 0 ? Because in our loop variable i runs from 1 to n. If n = 0 then of course the loop will not run once and then our fact remains 1. So the program is still true

Back consider little nhé. You try entering n is 20 or greater than see what happens? You will see the result is wrong or is returning a negative number that. The reason why? You note that due to the nature of the human consecutive factorial so its results will increase very fast. 5! = 120 but 6! = 720 it's gone. Therefore the fact of us will grow quickly make data types (int) our being unable to contain (You can see limited range of data types). So we need to change the data type. N Depending on the magnitude of which we estimate the data type we use is what type. You can use long or long long for all the factorial nhé.

/*
* Calculate n!
*/

#include <stdio.h>

int main()
{
	int i, n;
	long long fact = 1;

	printf("Enter n = ");
	scanf("%d", &n);

	for(i = 1; i <= n; i++) 
	{
		fact = fact * i;
	}

	printf("%d! = %lld\n", n, fact); // lld for long long

	return 0;
}

When this you can calculate 20!. And the following results:

Enter n = 20
20! = 2432902008176640000

20! are some very large.

Write a program to calculate n! by recursive. With non-negative integer n is entered from the keyboard

Apart from using loop out, we can use recursive function with nature to calculate the factorial. Have you noticed the formula for our factorial.

  • 0! = 1
  • n! = 1.2.3…n = approx.(A-1).(n-2)…2.1 = n.(A-1)!

Means n! = n.(A-1)!. In factorial definition has factorial, It is equivalent to recursive – recall that the main function of the function. We can write the program as follows:

/*
* Calculate n! by recursive
*/

#include <stdio.h>

long long fact(int n) 
{
	if(n == 0) return 1;
	return n * fact(n-1);
}

int main()
{
	int n;

	printf("Enter n = ");
	scanf("%d", &n);

	printf("%d! = %lld\n", n, fact(n));

	return 0;
}

You can refer to here about using function and recursive function.

Exercise: Write a program that displays 1!, 2!, 3!,…, 20! same time. Each line displays some.