Arifmetik operatorlar | C++
Wahid Abduhakimov
Posted on March 12, 2024
- arifmetik operatorlar:
+
,=
,*
,/
,%
(modulus) - ikkita operand ustida amal bajaradi
Qo'shish/Ayrish
int onbesh = 15;
int sakkiz = 8;
cout << onbesh + sakkis << end; // 23
cout << onbesh - sakkis << end; // 7
Butun sonlarni kasr sonlarga qo'shish/ayrishda natija kasr son bo'ladi.
int radius = 5;
float PI = 3.14;
cout << PI + radius << endl; // 8.14
Ko'paytirish/Bo'lish
- Butun sonlarni kasr sonlarga ko'paytirsa natija kasr son bo'ladi.
int tomoni = 4, balandligi = 5;
int yuzi = tomoni * balandligi;
-
/
(bo'lish) operatori butun sonni butun songa bo'lsa natija butun son bo'ladi. Ma'lumot yo'qolish havfi bor.
int son = 10;
float natija = son / 3; // natija = 3.0; ma'lumot yo'qotildi
float kasrNatija = 10 / 3.0; // kasrNatija = 3.33333;
Modulus/Qoldiq operatori
-
%
operatori bo'linmadan keyin qoldiqni hisoblaydi - faqat butun sonlar ustida ishlaydi
Masalan:
10 % 3
ifodasining natijasi1
ga teng.10
ni3
ga bo'lsak, butun qismi3
ga va qoldiq1
ga teng bo'ladi.
int son = 10;
long yanaSon = 12;
cout << son % 3 << endl; // 1
cout << yanaSon % 6 << endl; // 0
O'zlashtirish operatori (=)
- o'zgaruvchiga qiymat o'zlashtirish uchun ishlatiladi
- o'zlashtirish operatori (
=
) chap tarafida doimo o'zgaruvchi turishi shart
200 = x; // error - chap tarafda qiymat turibdi
x + 1 = 200 // error - chap tarafda ifoda turibdi
To'g'ri o'zlashtirishga misollar:
x = 5;
x = y;
y = x + 10;
Mashq
Quyidagi ifodalar natijalarini taxmin qiling!
cout << 3 * 4 << endl;
cout << 2.0 * 3.0 << endl;
cout << 2.0 * 4 << endl;
cout << 10 / 2 << endl;
cout << 10 / 3 << endl;
cout << 10 / 3.0 << endl;
cout << 10 % 3 << endl;
cout << 3.0 + 3 / 2 << endl;
💖 💪 🙅 🚩
Wahid Abduhakimov
Posted on March 12, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.