Arithmetic Operators พื้นฐาน 5 ตัว
- Addition (+)
- Subtraction (-)
- Division (/)
- Multiplication (*)
- Modulo (%)
ใช้ได้กับ arithmetic operations ทั้งหมดใน C++
ตัวอย่างโปรแกรมครบ
#include <iostream>
using namespace std;
int main() {
int a = 21;
int b = 10;
int c;
c = a + b;
cout << "Line 1 - Value of c is: " << c << endl;
c = a - b;
cout << "Line 2 - Value of c is: " << c << endl;
c = a * b;
cout << "Line 3 - Value of c is: " << c << endl;
c = a / b;
cout << "Line 4 - Value of c is: " << c << endl;
c = a % b;
cout << "Line 5 - Value of c is: " << c << endl;
return 0;
}
ผลลัพธ์
Line 1 - Value of c is: 31 Line 2 - Value of c is: 11 Line 3 - Value of c is: 210 Line 4 - Value of c is: 2 Line 5 - Value of c is: 1
หมายเหตุ: การหาร integer จะตัดทศนิยม 21/10 = 2, modulo 21%10 = 1