DEV Community

Cover image for C# da Arefmetik operators
islomAli99
islomAli99

Posted on

C# da Arefmetik operators

Assalamu aleykum hurmatli dasturchilar, bugun sizlar bilan C# dasturlash tilida Arefmetik operatsiyalar haqida to'liq gaplashamiz.

C# tilida arifmetik amallar bir operand (unary), ikki operand (binary), va uch operand (ternary) ustida bajariladi. Keling, barchasini ko'rib chiqamiz.

Binary Operations (Ikki operandli amallar)
+ - Ikki sonni qo'shish

int x = 10;
int z = x + 12; // 22

Enter fullscreen mode Exit fullscreen mode

- - Ikki sonni ayirish

int x = 10;
int z = x - 6; // 4

Enter fullscreen mode Exit fullscreen mode

* - Ikki sonni ko'paytirish

int x = 10;
int z = x * 5; // 50

Enter fullscreen mode Exit fullscreen mode

/ - Ikki sonni bo'lish

int x = 10;
int z = x / 5; // 2

double a = 10;
double b = a / 3; // 3.3333333333333335

Enter fullscreen mode Exit fullscreen mode

Agar ikkala operand butun son bo'lsa, natija ham butun son bo'ladi va u yaxlitlanadi.

double x = 10 / 4; // 2

Enter fullscreen mode Exit fullscreen mode

Ushbu muammodan chiqish uchun operandlarni double yoki float sifatida aniqlash kerak:

double x = 10.0 / 4.0; // 2.5

Enter fullscreen mode Exit fullscreen mode

% - Ikki sonning bo'lishdan qolgan qoldig'ini olish.

double x = 10.0 % 4.0; // 2

Enter fullscreen mode Exit fullscreen mode

Unary Operations (Bir operandli amallar)
++ - Inkrement (o'sish)

Prefiks ⇒ ++x - avval x qiymati 1 ga oshiriladi, keyin natija qaytariladi.
Postfiks ⇒ x++ - avval x qiymati qaytariladi, keyin 1 ga oshiriladi.

int x = 5;
int y = ++x;
Console.WriteLine($"{x} - {y}"); // x = 6, y = 6

int n = 5;
int m = n++;
Console.WriteLine($"{n} - {m}"); // n = 6, m = 5

Enter fullscreen mode Exit fullscreen mode

-- - Dekrement (kamayish)

Dekrement ham prefiks --x va postfiks x-- shakllarida mavjud.

Amallarni bajarish tartibi
Inkrement (++), dekrement (--)

Ko'paytirish (*), bo'lish (/), qoldiq olish (%)

Qo'shish (+), ayirish (-)

int a = 3;
int b = 5;
int c = 40;
int d = c-- - b * a;

Console.WriteLine($"a={a}; b={b}; c={c}; d={d}"); // a = 3; b = 5; c = 39; d = 25

Enter fullscreen mode Exit fullscreen mode

Top comments (0)