Basics · 04
Arithmetic and division
Add, subtract and multiply work the same everywhere — division doesn't.
+ - * work the same everywhere, but division and the remainder (%) differ between languages.
The example below takes two numbers and prints their sum, difference, product, quotient, remainder and fractional division.
a, b = map(int, input().split())print(a + b)print(a - b)print(a * b)print(a // b)print(a % b)print(a / b)const [a, b] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log(a + b);console.log(a - b);console.log(a * b);console.log(Math.trunc(a / b));console.log(a % b);console.log(a / b);import { readFileSync } from "node:fs";const [a, b]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log(a + b);console.log(a - b);console.log(a * b);console.log(Math.trunc(a / b));console.log(a % b);console.log(a / b);import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(), b = sc.nextInt(); System.out.println(a + b); System.out.println(a - b); System.out.println(a * b); System.out.println(a / b); System.out.println(a % b); System.out.println((double) a / b); }}using System; class Program{ static void Main() { string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int a = int.Parse(p[0]), b = int.Parse(p[1]); Console.WriteLine(a + b); Console.WriteLine(a - b); Console.WriteLine(a * b); Console.WriteLine(a / b); Console.WriteLine(a % b); Console.WriteLine((double)a / b); }}#include <iostream> int main() { int a, b; std::cin >> a >> b; std::cout << a + b << '\n'; std::cout << a - b << '\n'; std::cout << a * b << '\n'; std::cout << a / b << '\n'; std::cout << a % b << '\n'; std::cout << (double)a / b << '\n';}#include <stdio.h> int main(void) { int a, b; scanf("%d %d", &a, &b); printf("%d\n", a + b); printf("%d\n", a - b); printf("%d\n", a * b); printf("%d\n", a / b); printf("%d\n", a % b); printf("%g\n", (double)a / b); return 0;}nums = Console.ReadNumbers();a = nums[0];b = nums[1];Console.WriteLine(a + b);Console.WriteLine(a - b);Console.WriteLine(a * b);Console.WriteLine(Mathf.Quot(a, b));Console.WriteLine(a % b);Console.WriteLine(a / b);7 2
9 5 14 3 1 3.5
Key points
- In some languages integer ÷ integer is an integer:
7 / 2→3(the fraction is dropped).
Languages with a single number type give3.5right away. - For a fractional result, convert one side to a floating-point number before dividing.
- With negative numbers, languages split into two camps: those that truncate toward zero (
-3.5→-3) and those that floor (-3.5→-4). - Integers have a limit.
When they overflow, you may get a wrong value with no error at all. - Dividing by zero stops some languages with an error; others quietly give
Infinity.
How each language writes it
| Python | // is the quotient (7 // 2 → 3), / always gives a float (7 / 2 → 3.5; even 6 / 2 is 3.0).Negatives floor. Integers never overflow. Dividing by zero raises ZeroDivisionError |
|---|---|
| JavaScript | One number type, so 7 / 2 → 3.5.The quotient is Math.trunc(a / b); Math.floor differs for negatives (Math.floor(-3.5) → -4).Beyond 2^53 the last digits go wrong ( 9007199254740993 → 9007199254740992).Dividing by zero gives Infinity |
| TypeScript | Same as JavaScript |
| Java | Integer ÷ integer is an integer; for a fraction use (double)a / b → 3.5.Truncates toward zero. An int overflow silently wraps around; use long for big numbers.Integer division by zero throws ArithmeticException |
| C# | Same as Java. Integer division by zero throws DivideByZeroException |
| C++ | Integer ÷ integer is an integer; for a fraction use (double)a / b.Truncates toward zero. Signed overflow and division by zero are undefined behavior. Use long long for big numbers |
| C | Same as C++ |
| Craykon | One number type, so 7 / 2 → 3.5.The quotient is Mathf.Quot(a, b), floor is Mathf.Floor.% truncates toward zero.Beyond 2^53 the last digits go wrong, and dividing by zero gives Infinity |
Common mistakes
Checking odd numbers with x % 2 == 1Java · C# · C · C++ · JavaScript · TypeScript · Craykon
int x = -3;
printf("%d\n", x % 2);→ -1 — so x % 2 == 1 is false and negative odd numbers are missed
Write x % 2 != 0.
It is safe in every language.
Converting after dividingJava · C# · C · C++
int a = 7, b = 2;
double d = (double)(a / b);→ 3.0 — the integer division inside the parentheses finishes first
Convert before dividing: (double)a / b → 3.5.
int overflowJava · C# · C · C++
int x = 2147483647;
x = x + 1;→ Java and C# silently give -2147483648. In C and C++ it is undefined behavior
Use long (long long in C and C++) for big numbers.
How languages differ
Negative division differs between languages.
Python gives
Negative division differs between languages.
Python gives
-7 // 2 → -4 and -7 % 2 → 1 (floor), while C, C++, Java, C#, JavaScript (Math.trunc) and Craykon (Mathf.Quot) give quotient -3 and remainder -1 (truncate toward zero).