在 C++ 中对两个数字进行加法
加法是一种基本的算术运算。对两个数字进行加法的程序执行两个数字的加法运算,然后将它们的和显示在屏幕上。
以下是演示对两个数字进行加法的程序:
示例
#include <iostream>
using namespace std;
int main() {
int num1=15 ,num2=10, sum;
sum = num1 + num2;
cout<<"Sum of "<<num1<<" and "<<num2<<" is "<<sum;
return 0;
}输出
Sum of 15 and 10 is 25
在上述程序中,两个数字的和(即 15 和 10)存储在变量 sum 中。
sum = num1 + num2;
然后使用 cout 对象将其显示在屏幕上。
cout<<"Sum of "<<num1<<" and "<<num2<<" is "<<sum;
以下是使用数组存储两个数字及其和的程序:
示例
#include <iostream>
using namespace std;
int main() {
int a[3];
a[0]=7;
a[1]=5;
a[2]=a[0] + a[1];
cout<<"Sum of "<<a[0]<<" and "<<a[1]<<" is "<<a[2];
return 0;
}输出
Sum of 7 and 5 is 12
在上述程序中,要进行加法的数字存储在数组的 0 和 1 索引中。
a[0]=7; a[1]=5;
然后,将和存储在数组的 2 索引中。
a[2]=a[0] + a[1];
使用 cout 对象将和显示在屏幕上。
cout<<"Sum of "<<a[0]<<" and "<<a[1]<<" is "<<a[2];
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP