C语言入门练习— —累乘与累加混合计算

累加与累乘的混合计算—“ + - × ”
printf("1.累加与累乘的加减 \n a+...+b  +  e*...*f=all \n a+...+b -  e*...*f=all \n e*...*f - a+...+b =all \n\n ");
	int a, b, c = 0, sum = 0;
	printf("请输入两个整数进行累加:");
	scanf_s("%d %d", &a, &b);
	if (a < b)
	{
     
		for (c = a; c <= b; c++)
		{
     
			sum += c;
		}
		printf("%d到%d的累加值为:%d\n", a, b, sum);
	}

	else
	{
     
		if (a == b)
		{
     
			printf("请输入两个不相等的值");

		}
		else
		{
     
			for (c = a; c >= b; c--)
			{
     
				sum += c;
			}
			printf("%d到%d的累加值为:%d\n", a, b, sum);
		}
	}

	int e,f, g = 1, sum_c = 1;
	printf("请输入两个整数进行累乘:");
	scanf_s("%d %d", &e, &f);
	if (e < f)
	{
     
		for (g = e; g <= f; g++)
		{
     
			sum_c*= g;
		}
		printf("%d到%d的累乘值为:%d\n", e, f, sum_c);
	}

	else
	{
     
		if (e == f)
		{
     
			printf("请输入两个不相等的值");

		}
		else
		{
     
			for (g = e; g <= f; g--)
			{
     
				sum_c *= g;
			}
			printf("%d到%d的累乘值为:%d\n", e, f, sum_c);
		}
	}
	printf("%d到%d的累加与%d到%d的累乘的和为:%d\n",a,b,e,f,sum+sum_c);
	printf("%d到%d的累加与%d到%d的累乘的差为:%d\n", a, b, e, f, sum - sum_c);
	printf("%d到%d的累乘与%d到%d的累加的差为:%d\n", e,f,a, b, sum_c - sum);

与上章很像,但是计算是要注意运算规律

C语言入门练习— —累乘与累加混合计算_第1张图片


	printf("2.累加与累乘的乘除 \n a+...+b  ×  e*...*f=all \n a+...+b ÷  e*...*f=all \n e*...*f × a+...+b =all \n e*...*f ÷ a+...+b =all \n\n ");
	int all_c, all_kc, first_d,first_e;
	int  a_d, b_d, c_d = 0, sum_d = 0, last_d = 0;
	printf("请输入两个整数进行累加:");
	scanf_s("%d %d", &a_d, &b_d);
	if (a_d < b_d)
	{
     
		for (c_d = a_d; c_d <= b_d; c_d++)
		{
     
			sum_d += c_d;
		}
		last_d = b_d;
		first_d = a_d;
		printf("%d到%d的累加值为:%d\n", a_d, b_d, sum_d);
	}

	else
	{
     
		if (a_d == b_d)
		{
     
			printf("请输入两个不相等的值");

		}
		else
		{
     
			for (c_d = a_d; c_d >= b_d; c_d--)
			{
     
				sum_d += c_d;
			}
			last_d = b_d;
			first_d = a_d;
			printf("%d到%d的累加值为:%d\n", a_d, b_d, sum_d);
		}

	}
	

	int e_d, f_d, g_d = 1, sum_e = 1, last_e = 1;
	printf("请输入两个整数进行累乘:");
	scanf_s("%d %d", &e_d, &f_d);
	if (e_d < f_d)
	{
     
		for (g_d = e_d; g_d <= f_d; g_d++)
		{
     
			sum_e *= g_d;
		}
		last_e = f_d;
		first_e = e_d;
		printf("%d到%d的累乘值为:%d\n", e_d, f_d, sum_e);
		

	}

	else
	{
     
		if (e_d == f_d)
		{
     
			printf("请输入两个不相等的值");

		}
		else
		{
     
			for (g_d = e_d; g_d >= f_d; g_d--)
			{
     
				sum_e *= g_d;
			}
			last_e = e_d;
			first_e = f_d;
			printf("%d到%d的累乘值为:%d\n", e_d, f_d, sum_e);

		}
	}
	
	all_c = sum_d - last_d + sum_e *last_d ;
	all_kc = sum_e *first_d+ sum_d - first_d;

	printf("%d+...+%d  ×  %d*...*%d的累加和累乘的混合计算为:%d\n", a_d, b_d, e_d, f_d, all_c);
	printf("%d*...*%d  ×  %d+...+%d的累加和累乘的混合计算为:%d\n", e_d, f_d, a_d, b_d, all_kc);
	

中间的✖号需要与前后相乘 因此有所不同

C语言入门练习— —累乘与累加混合计算_第2张图片

代码可能有点冗余,见谅
初学入门练习题,后期进行改进

你可能感兴趣的:(c语言)