cppAcademy
← Learning PathLESSON 3 / 30 · FUNDAMENTALS · 25 MIN✕

Data Types and Operators

Types determine what values a variable can represent and how operations behave. An integer count, an approximate real measurement, a single character and a true/false decision need different representations. Competitive programs often fail because arithmetic overflows or integer division silently truncates an intended fraction. This example divides two nonnegative integers and shows exact integer results beside an approximate decimal ratio.

Prerequisites: C++ Basics and Program Structure.

Build the idea

  1. 1. Choose a representation deliberately

    int is convenient for bounded indices and small counts; long long has at least 64 bits and is useful for larger totals. double represents approximate real values, so it cannot store every decimal exactly. char stores one character such as 'A', while bool stores false or true. Exact type sizes depend on the implementation: do not treat “int is always four bytes” as a language guarantee. Inspect limits and sizeof when portability matters.

  2. 2. Distinguish arithmetic operations

    Addition, subtraction and multiplication use +, − and *. With integer operands, / discards the fractional part toward zero. % returns a remainder; for nonnegative a and positive b, a=(a/b)*b+a%b and 0≤a%b<b. For signed inputs, the remainder can be negative. Division or remainder by zero is invalid. This program requires b>0 so every demonstrated operation is defined.

  3. 3. Convert BEFORE the risky operation

    For 7/2, both operands are integers, so assigning that result to double still gives 3.0. Converting one operand first, static_cast<double>(a)/b, produces approximately 3.5. Likewise long long product=1LL*x*y widens before multiplying two int values; merely assigning x*y afterwards cannot repair prior signed overflow. Signed integer overflow is undefined behavior, not guaranteed wraparound.

  4. 4. Form comparisons and logic

    Relational operators <, >, <= and >= compare values; == tests equality and != inequality. Logical && means AND, || means OR and ! negates a Boolean. The expression (a%b==0) asks whether division is exact. These operations yield bool. In normal numeric output cout prints false as 0 and true as 1 unless boolalpha is enabled.

  5. 5. Read the displayed results

    For a=7,b=2 the quotient is 3 and remainder 1. Converting before division gives a ratio printed with three digits after the decimal point: 3.500. The final Boolean is 0 because the remainder is nonzero. fixed and setprecision control output formatting, not the stored precision of double. Use integers for exact counts and money-like scaled units whenever the problem permits.

C++17 implementation

This is a complete program. Compile with C++17 and enter the input described below.

#include<iostream>
#include<iomanip>
int main(){
    long long a,b;
    std::cin>>a>>b; // Contract: a>=0 and b>0.
    std::cout<<a/b<<' '<<a%b<<'\n';
    double ratio=static_cast<double>(a)/b;
    std::cout<<std::fixed<<std::setprecision(3)<<ratio<<'\n';
    bool divisible=(a%b==0);
    std::cout<<divisible<<'\n';
}

Complexity analysis

O(1) time and O(1) space for fixed-width arithmetic and a fixed number of outputs. Floating-point output is approximate; integer quotient/remainder remain exact within the input contract.

Common mistakes and edge cases. Use == for comparison and = for assignment. Do not divide by zero, expect negative remainders to be positive, or assume double is exact. Parenthesize compound arithmetic when intent is unclear. Widen operands before multiplication. a and b here are at most 10^9, so quotient/remainder and conversion are safe; larger values may lose integer precision when converted to double.

Example input and output

Integers a b with 0≤a≤10^9 and 1≤b≤10^9. Print quotient/remainder, decimal ratio to three places, then whether a is divisible by b.

Input

7 2

Output

3 1
3.500
0

7=3×2+1. The decimal ratio is 3.5, but the division is not exact.

Practical applications

Choose int for small bounded values, long long for totals/products that may exceed int, bool for conditions, char for symbols, and double when the answer is intrinsically approximate with a tolerance. Later lessons use these choices in sums, counts, distances and sorting keys.

Check your understanding

What is static_cast<double>(7/2)?

Put it into practice

Test 0/5, 8/2 and 1/3.

Show a hint

The ratio of 1/3 is rounded for display; exact divisibility still uses remainder.