十个整数相加怎么编程

时间:2025-01-27 05:11:29 网络游戏

C语言

使用数组

```c

include

int main() {

int a;

int sum = 0;

int i;

printf("请输入10个整数:\n");

for (i = 0; i < 10; i++) {

scanf("%d", &a[i]);

sum += a[i];

}

printf("这十个数的和为: %d\n", sum);

return 0;

}

```

使用循环和累加

```c

include

int main() {

int sum = 0;

int i;

printf("请输入10个整数:\n");

for (i = 0; i < 10; i++) {

scanf("%d", &i);

sum += i;

}

printf("这十个数的和为: %d\n", sum);

return 0;

}

```

Java

```java

import java.util.Scanner;

public class TestOddAdd {

public static void main(String[] args) {

int sum = 0;

int[] a = new int;

Scanner scanner = new Scanner(System.in);

System.out.println("请输入10个整数:");

for (int i = 0; i < 10; i++) {

a[i] = scanner.nextInt();

sum += a[i];

}

System.out.println("这十个数的和为: " + sum);

}

}

```

Python

```python

sum = 0

for i in range(10):

num = int(input("请输入一个整数: "))

sum += num

print("这十个数的和为:", sum)

```

C++

```cpp

include

using namespace std;

int main() {

int sum = 0;

int a;

cout << "请输入10个整数:" << endl;

for (int i = 0; i < 10; i++) {

cin >> a[i];

sum += a[i];

}

cout << "这十个数的和为: " << sum << endl;

return 0;

}

```

JavaScript

```javascript

let sum = 0;

for (let i = 0; i < 10; i++) {

let num = parseInt(prompt("请输入一个整数:"));

sum += num;

}

console.log("这十个数的和为:", sum);

```

这些示例展示了如何在不同的编程语言中实现十个整数的相加。你可以选择最适合你的语言和环境的方法。