The exact value of factorial in C language

Source: Internet
Author: User

For a large number of operations, may exceed the range of int, or even long, for this, you can use an array to store large numbers, the following code is to ask for 1000 of the number of factorial code, the code is as follows:

#include <stdio.h>#include<string.h>Const intmax= the;intMain () {intf[ the];//Store the final result    intI,j,n,c,tem; Memset (F,0,sizeof(f)); scanf ("%d",&N); f[0]=1;  for(i=2; i<=n;i++) {C=0;  for(j=0; j<max;j++) {tem=i*f[j]+C; C=tem/Ten; F[J]=tem%Ten; }    }     for(j=max-1; j>=0; j--)        if(f[j]!=0)             Break;  for(i=j;i>=0; i--) printf ("%d", F[i]); printf ("\ n"); return 0;}

The understanding of this code can be explained by the simplest examples.

For example, using an int array to store the result, the array length is 4, and the array name is a.

In the 45*33 operation, the written calculation method of using primary school is as follows:

In the computer language, we calculate this, first a[0]=5,a[1]=4,a[3]=0,a[4]=0;

33*a[0]=135, A[0] is assigned a value of 5, carry is c=13

Step Two

33*a[1]+c=148, A[1] is assigned a value of 8, carry is c=14

Step Three

33*a[2]+c=14, A[2] is assigned a value of 4, carry is c=1

Fourth Step

33*a[3]+c=1, now the A[3] assignment is 1, the operation succeeds!

The exact value of factorial in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.