अंकगणितीय ऑपरेटर्स
Arithmetic operators calculator के buttons हैं: add, subtract, multiply, divide, और remainder निकालना। Scala इनका उपयोग numbers और variables से math करने के लिए करता है।
In this page:
Syntax
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
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
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- दो
Intvalues पर/का उपयोग करना और decimal की उम्मीद करना, जबकि7 / 23है;7.0 / 2का उपयोग करें। - किसी
Intको0से divide करना, जोArithmeticExceptionफेंकता है, जबकिDoubleInfinityदेता है। - exponents के लिए
^का उपयोग करना, जबकि यह bitwise XOR है और आपकोmath.pow(a, b)चाहिए।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: