DEV Community

islomAli99
islomAli99

Posted on • Updated on

Arifmetik operatorlar | C++

  • arefmetik operatorlar: +, -, *, /, % (modulus)

  • ikkita operand ustida amal bajaradi.

Qo'shish/Ayirish

#include <iostream>

using namespace std;

int main()
{

   int onbesh = 15;
   int sakkiz = 8;

   cout << onbesh + sakkiz << endl; //23
   cout << onbesh - sakkiz << endl; //7

  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Butun sonlarni kasr sonlarga qo'shish/ayrishda natija kasr son bo'ladi.

#include <iostream>

using namespace std;

int main()
{

  int radius = 5;
  float PI = 3.14;

  cout << PI + radius << endl; // 8.14

  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Ko'paytirish/Bo'lish

  • Butun sonlarni kasr sonlarga ko'paytirsa natija kasr son bo'ladi.
#include <iostream>

using namespace std;

int main()
{

   int tomoni = 4, balandligi = 5;
   int yuzi = tomoni * balandligi;

   cout << "Area is " << yuzi << "." << endl;

  return 0;
}
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.
#include <iostream>

using namespace std;

int main()
{

   int son = 10;
   float natija = son / 3; // natija = 3 chiqadi, butun sonnni butun songa bo'lganimiz uchun

    cout << natija << endl; // 3
   float kasrNatija = 10 / 3.0;

   cout << kasrNatija << endl; // natija: 3.3333 chiqadi, sabab butun sonni kasr songa bo'lganda natija kasr son chiqadi.


  return 0;
}
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 1 ga teng. 10 ni 3 ga bo'lsak, butun qismi 3 ga va qoldiq 1 ga teng bo'ladi.

#include <iostream>

using namespace std;

int main()
{

   int son = 10;
   long yanaSon = 12;

   cout << son % 3 << endl;     // 1
   cout << yanaSon % 6 << endl; // 0

  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)