Back to track
#79 Easy Foundations

Basic Types

Open the editor

Problem

Read an int, a double, a char and a bool (as 0 or 1), then print them on one line separated by single spaces. The double must print with exactly two decimals.

Constraints

The double fits in a standard double; the char is a single visible character.

Input

Line 1: int, double, char, bool — space separated.

Output

One line: the four values, space separated, double to 2 decimals, bool as 0 or 1.

The idea

Concept

Each fundamental type has its own stream behaviour: bool prints as 1/0 unless boolalpha is set, and a double prints with 6 significant digits until you fix the format. Reading a bool with cin >> only accepts 0 or 1 by default.

Approach

Extract in order, then set fixed and setprecision(2) before printing the double.

Hints

  1. 1

    cout << fixed << setprecision(2) fixes the decimal count.

  2. 2

    A bool streams as 1 or 0 by default — that is what is wanted here.

  3. 3

    Extraction order must match the input order exactly.

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);

  int i;
  double d;
  char c;
  bool b;
  if (!(cin >> i >> d >> c >> b)) return 0;
  cout << i << ' ' << fixed << setprecision(2) << d << ' ' << c << ' ' << b << '\n';
  return 0;
}

© 2026 ChitChat