用C++写一个计算1+10+5X9+1的过程和结果
时间: 2024-02-05 12:04:53 浏览: 23
#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+10+5*9+1的过程和结果
#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+2+3+…+10的程序
以下是用C++写一个1+2+3+…+10的程序的代码:
```
#include <iostream>
using namespace std;
int sum(int n) {
int x = n;
x && (x += sum(n-1));
return x;
}
int main() {
cout << "1+2+3+...+10=" << sum(10) << endl;
return 0;
}
```
这个程序使用了递归的方法来求和,避免了使用循环和条件判断语句。函数sum(n)表示求1+2+3+...+n的和,如果n为0,则返回0,否则返回n加上sum(n-1)的结果。在main函数中,我们调用sum(10)来求1+2+3+...+10的和,并输出结果。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)