C# da Arefmetik operators
islomAli99
Posted on July 31, 2024
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
-
- Ikki sonni ayirish
int x = 10;
int z = x - 6; // 4
*
- Ikki sonni ko'paytirish
int x = 10;
int z = x * 5; // 50
/
- Ikki sonni bo'lish
int x = 10;
int z = x / 5; // 2
double a = 10;
double b = a / 3; // 3.3333333333333335
Agar ikkala operand butun son bo'lsa, natija ham butun son bo'ladi va u yaxlitlanadi.
double x = 10 / 4; // 2
Ushbu muammodan chiqish uchun operandlarni double yoki float sifatida aniqlash kerak:
double x = 10.0 / 4.0; // 2.5
%
- Ikki sonning bo'lishdan qolgan qoldig'ini olish.
double x = 10.0 % 4.0; // 2
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
--
- 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
Posted on July 31, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.