int main(void)\n{ \n int a = 2, b = -1, c = 2;\n\n if(a < b)\n if(b < 0)
时间: 2023-05-02 21:02:37 浏览: 70
这是一个C语言的程序,开始定义了三个整型变量a,b,c。程序接下来使用if语句,首先判断a是否小于b,如果成立再判断b是否小于0。但是这段代码缺少else语句,无法得知如果if语句判断不成立时程序应该做什么。
相关问题
用递归方法算S=1+2+3+…+n
以下是两种语言的递归方法求解S=1+2+3+...+n的和:
- C++代码:
```cpp
#include <iostream>
using namespace std;
int funSum(int n) {
if(n == 1) return 1;
else return n + funSum(n-1);
}
int main() {
int n, sum;
cout << "input n: ";
cin >> n;
sum = funSum(n);
cout << "the sum is: " << sum << endl;
return 0;
}
```
- Java代码:
```java
public class Sum {
public static int funSum(int n) {
if(n == 1) return 1;
else return n + funSum(n-1);
}
public static void main(String[] args) {
int n = 10;
int sum = funSum(n);
System.out.println("the sum is: " + sum);
}
}
```
用c语言求s=1!+2!+...+n!
以下是用C语言求s=1!+2!+...+n!的代码实现:
```c
#include <stdio.h>
int main(void) {
int n = 0;
int i = 0;
int m = 1;
int sum=0;
printf("请输入n的值:"); scanf("%d", &n);
for (i=1; i<=n; ++i) {
m = m * i;
sum = sum +m;
}
printf ("sum = %d\n", sum);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](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)