两数相加编程语言怎么写

时间:2025-01-26 22:39:06 网络游戏

Python:

```python

输入两个数

num1 = int(input("请输入第一个数:"))

num2 = int(input("请输入第二个数:"))

计算两个数的和

sum = num1 + num2

输出结果

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

```

Java:

```java

public class AddTwoNumbers {

public static void main(String[] args) {

int num1 = 5;

int num2 = 3;

int sum = num1 + num2;

System.out.println("The sum is: " + sum);

}

}

```

C++:

```cpp

include

using namespace std;

int main() {

int a = 5;

int b = 3;

int sum = a + b;

cout << "The sum is: " << sum << endl;

return 0;

}

```

C语言:

```c

include

int main() {

int num1 = 5, num2 = 10;

int sum = num1 + num2;

printf("两数之和为:%d\n", sum);

return 0;

}

```

JavaScript (Node.js):

```javascript

const readline = require('readline');

const rl = readline.createInterface({

input: process.stdin,

output: process.stdout

});

rl.question('请输入第一个数: ', function(num1) {

rl.question('请输入第二个数: ', function(num2) {

const sum = parseInt(num1) + parseInt(num2);

console.log('两个数的和为:', sum);

rl.close();

});

});

```

Dart:

```dart

void main() {

int num1 = 10;

int num2 = 20;

int sum = num1 + num2;

print('The sum of $num1 and $num2 is $sum');

}

```

这些示例展示了如何在不同的编程语言中实现两数相加的功能。你可以根据所使用的编程语言选择合适的代码片段进行尝试。