← Back to Scala Course | Chapter 3: Operators & Expressions | Lesson 1 of 7

अंकगणितीय ऑपरेटर्स

Arithmetic operators calculator के buttons हैं: add, subtract, multiply, divide, और remainder निकालना। Scala इनका उपयोग numbers और variables से math करने के लिए करता है।

In this page:

  1. Arithmetic Operators
Syntax
scala
a + b
a - b
a * b
a / b
a % b

Arithmetic Operators

Scala arithmetic के लिए +, -, *, /, और % प्रदान करता है, जो सभी numeric types पर methods के रूप में implement किए गए हैं (Scala में वास्तव में कोई खास-स्थिति वाले operators नहीं हैं)।

Integer division किसी भी fractional part को truncate कर देता है, जबकि दो Doubles को divide करने पर precision बनी रहती है। % division का remainder return करता है।

Warning: दो Ints को divide करना, जैसे 7 / 2, 3 देता है, 3.5 नहीं -- fractional result पाने के लिए कम से कम एक operand को Double बनाएं।

उदाहरण: Arithmetic Operators

markup
object Main extends App {
  val a = 7
  val b = 2

  println(a + b)
  println(a - b)
  println(a * b)
  println(a / b)          // integer division
  println(a % b)          // remainder
  println(a.toDouble / b) // fractional division
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. दो Int values पर / का उपयोग करना और decimal की उम्मीद करना, जबकि 7 / 2 3 है; 7.0 / 2 का उपयोग करें।
  2. किसी Int को 0 से divide करना, जो ArithmeticException फेंकता है, जबकि Double Infinity देता है।
  3. exponents के लिए ^ का उपयोग करना, जबकि यह bitwise XOR है और आपको math.pow(a, b) चाहिए।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.