Back to track
#89 Easy Foundations

Arithmetic Operators

Open the editor

Problem

Read two integers a and b and print a+b, a-b, a*b, a/b and a%b, space separated on one line.

Constraints

-10^9 <= a, b <= 10^9, b != 0

Input

Line 1: two integers a and b.

Output

One line: five results, space separated.

The idea

Concept

Integer / truncates toward zero and % takes the sign of the dividend, so -7 % 2 is -1 rather than 1. That sign rule is what breaks naive modular-arithmetic code on negative inputs.

Approach

Compute all five in 64-bit and print in order.

Hints

  1. 1

    Use long long so a*b cannot overflow.

  2. 2

    % follows the sign of the left operand.

  3. 3

    / truncates toward zero, not toward negative infinity.

Worked solution

Reference solution

Time: O(1) · Space: O(1)

One correct implementation. Any approach that satisfies the constraints is equally valid.

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  long long a, b;
  if (!(cin >> a >> b)) return 0;
  cout << a + b << ' ' << a - b << ' ' << a * b << ' '
       << a / b << ' ' << a % b << '\n';
  return 0;
}

© 2026 ChitChat