使用 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
廣告