用C++写一个计算1+10+5*9+1的过程和结果
时间: 2024-05-19 20:16:02 浏览: 7
#include <stdio.h>
int main() {
int a = 1;
int b = 10;
int c = 5;
int d = 9;
int e = 1;
int result = a * b * c * d * e;
printf("1 * 10 * 5 * 9 * 1 = %d\n", result);
return 0;
}
// 输出结果:1 * 10 * 5 * 9 * 1 = 450
相关问题
用C++写一个计算1+10+5X9+1的过程和结果
#include <stdio.h>
int main() {
int num = 1;
printf("1");
for(int i = 1; i <= 10; i++) {
num *= 5;
num -= 9;
printf(" %d", num);
}
printf("\n");
printf("结果为%d\n", num);
return 0;
}
//输出
//1 -4 11 46 231 1156 5771 28856 144281 721396 3606981
//结果为3606981
c++求1+2!+3!+...+10!的和
这是一个C++程序,目的是计算1到10的阶乘之和。程序使用了for循环和变量来实现。具体实现过程如下:
1. 定义循环变量i、输入变量n、阶乘变量ret和和变量sum,并初始化ret和sum为1和0。
2. 通过scanf函数输入n的值。
3. 使用for循环从1到n,依次计算每个数的阶乘并将其加入sum中。
4. 输出sum的值。
下面是实现代码:
```
#include <stdio.h>
int main() {
int i = 0; //循环变量
int n = 0; //输入n
int ret = 1; //把每个数的阶乘计算后赋值给该变量,因为0乘任何数都为0,所以初始化为1。
int sum = 0; //外循环后将各个数的阶乘相加后赋值给该值,作为最终结果。
//输入:
scanf("%d", &n);//记得使用取地址符:&
//实现代码:
for (i = 1; i <= n; i++) //外循环:实现阶乘相加,相加到 n!
{
ret = ret * i;//依次相乘再赋值给变量ret
sum += ret; //将各数的阶乘相加后赋给该值
}
printf("%d", sum); //输出结果
return 0;
}
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)