编写加和的程序可以根据不同的编程语言有不同的实现方法。以下是几种常见编程语言的加和程序示例:
C语言
```c
include
int main() {
int a, b, sum;
a = 123;
b = 456;
sum = a + b;
printf("sum is %d\n", sum);
return 0;
}
```
Python
```python
num1 = int(input("请输入第一个数:"))
num2 = int(input("请输入第二个数:"))
sum = num1 + num2
print("两个数的和为:", sum)
```
Java
```java
public class SumExample {
public static void main(String[] args) {
int a = 5;
int b = 3;
int sum = a + b;
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; } ``` JavaScript ```javascript let a = 5; let b = 3; let sum = a + b; console.log("The sum is:", sum); ``` 函数式编程(Python) ```python def add_numbers(a, b): return a + b result = add_numbers(3, 5) print(result) 输出 8 ``` 多项式相加(C语言) ```c include int main() { int num1, num2, sum; printf("请输入两个数:\n"); scanf("%d%d", &num1, &num2); sum = num1 + num2; printf("sum=%d\n", sum); return 0; } ``` 阶乘累加和(C语言) ```c include int fun(int n) { if (n == 1) return 1; else return n * fun(n - 1); } int main() { int n, sum = 0, i; printf("请输入一个值,求1到n的累加值:\n"); scanf("%d", &n); for (i = 1; i <= n; i++) { sum += fun(i); } printf("累加和为:%d\n", sum); return 0; } ``` 这些示例展示了如何在不同的编程语言中实现加和的功能。你可以根据自己的需求和熟悉程度选择合适的编程语言进行编写。