C++ มีตัวดำเนินการเลขคณิตพื้นฐาน 5 ตัว พวกมันคือ −
- ส่วนเพิ่มเติม(+)
- การลบ(-)
- ดิวิชั่น(/)
- การคูณ(*)
- โมดูลัส(%)
ตัวดำเนินการเหล่านี้สามารถดำเนินการกับการดำเนินการทางคณิตศาสตร์ใดๆ ใน C++ มาดูตัวอย่างกัน −
ตัวอย่าง
#include <iostream>
using namespace std;
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