The factorial sum of n is calculated 1!+2!+3!+........+n!

n factorial: 1x2x3x4...xn

intmain()
{
	int n ;
	scanf("%d", &n);
	int sum = 1;
	int i = 1;
	for (; i <= n; i++) {
		sum=sum*i;
	}

	
	printf("%d\n", sum);
        system("pause");
	return 0;
}

Calculate 1!+2!+3!+........+n!

Here we call the function, construct a get_val() function to calculate the factorial, and the main() function calls this function to realize the calculation.

int get_val(int n)
{
	int i = 1;
	int sum = 1;
	for (; i <= n; i++) {
		sum = sum*i;
	}
	return sum;

}
intmain()
{
	int n ;
	scanf("%d", &n);
	int sum = 1;
	int i = 1;
	for (; i <= n; i++) {
            sum += get_val(i);
	}
	printf("%d\n", sum);

        system("pause");
	return 0;
}

Note: The format control of scanf cannot have commas, spaces, and when double defines a variable, the format control must be %lf.

         The variables i and sum that exist in get_val() and main() are not the same, because the variables defined in the function are temporary variables.

       

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686995&siteId=291194637