DEV Community

Cover image for Arifmetik operatorlar | C++
Wahid Abduhakimov
Wahid Abduhakimov

Posted on • Updated on

Arifmetik operatorlar | C++

  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Ko'paytirish/Bo'lish
  • Butun sonlarni kasr sonlarga ko'paytirsa natija kasr son bo'ladi.
int tomoni = 4, balandligi = 5;
int yuzi = tomoni * balandligi;
Enter fullscreen mode Exit fullscreen mode
  • / (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;
Enter fullscreen mode Exit fullscreen mode

Modulus/Qoldiq operatori
  • % operatori bo'linmadan keyin qoldiqni hisoblaydi
  • faqat butun sonlar ustida ishlaydi

Masalan: 10 % 3 ifodasining natijasi 1ga teng. 10ni 3ga bo'lsak, butun qismi 3ga va qoldiq 1ga teng bo'ladi.

int son = 10;
long yanaSon = 12;

cout << son % 3 << endl;       // 1
cout << yanaSon % 6 << endl;   // 0
Enter fullscreen mode Exit fullscreen mode

O'zlashtirish operatori (=)

Image description

  • 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    
Enter fullscreen mode Exit fullscreen mode

To'g'ri o'zlashtirishga misollar:

x = 5; 
x = y;
y = x + 10;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)