ในการเขียนโปรแกรม ตัวดำเนินการ ++ เป็นตัวดำเนินการเพิ่มที่เพิ่มค่าของตัวถูกดำเนินการ 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