使用 C++ 中的 ++ 运算符增加两个数字。
在编程中,++ 运算符是自增运算符,它将操作数的值增加 1。通过将 1 添加到数字 a b 次,我们可以使用此运算符增加两个数字。
例如,
Input: a = 31 , b = 4 Output: 35
解释 − 将 1 添加到 31 四次,总计 31 +1+1+1+1 = 35。
算法
Input: two integers a and b. Step 1: loop from 0 to b and follow step 2. Step 2: add 1 to b. Step 3: print the value of a.
示例
#include <iostream>
using namespace std;
int main(){
int x = 324 , y= 76;
cout<<"The sum of "<<x<<" & "<<y;
if(y>0){
for(int i= 0; i<y;i++){
x++;
}
} else {
for(int i= y; i<0;i++){
x--;
}
}
cout<<" is "<<x;
return 0;
}输出
The sum of 324 & 76 is 400
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP