计算加减法可以通过多种编程语言实现,以下是几种常见编程语言的示例代码:
Python
```python
简单的加减法
a = 10
b = 20
sum = a + b
difference = a - b
print("Sum:", sum)
print("Difference:", difference)
```
Java
```java
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
int difference = a - b;
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
}
}
```
C++
```cpp
include
int main() {
int a = 10;
int b = 20;
int sum = a + b;
int difference = a - b;
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
return 0;
}
```
JavaScript
```javascript
const a = 10;
const b = 20;
const sum = a + b;
const difference = a - b;
console.log("Sum:", sum);
console.log("Difference:", difference);
```
C
```csharp
using System;
class Program {
static void Main() {
int a = 10;
int b = 20;
int sum = a + b;
int difference = a - b;
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Difference: " + difference);
}
}
```
Ruby
```ruby
a = 10
b = 20
sum = a + b
difference = a - b
puts "Sum: {sum}"
puts "Difference: {difference}"
```
PHP
```php
<?php
$a = 10;
$b = 20;
$sum = $a + $b;
$difference = $a - $b;
echo "Sum: $sum\n";
echo "Difference: $difference\n";
?>
```
这些示例展示了如何在不同的编程语言中实现两个数的加法和减法运算。你可以根据需要选择合适的编程语言和库来进行更复杂的计算。