Menu
CoddyTech

Division and Remainders

Lesson 4 of 48 in Coddy's Python for C# Developers course.

Choose numeric operations deliberately when porting C# arithmetic.

C# reference excerpt

Executable statements use C# top-level program style; type-only examples are declarations. System names assume the usual using System directive.

Console.WriteLine(-7 / 3);   // -2
Console.WriteLine(-7 % 3);   // -1
Console.WriteLine(7 / 2);    // 3

Python

print(-7 // 3)  # -3
print(-7 % 3)   # 2
print(7 / 2)    # 3.5

What changes

C# integer division truncates toward zero. Python / produces a floating-point quotient for integer operands; // floors toward negative infinity. Python remainder follows the divisor sign for nonzero integer remainders, while C# remainder follows the dividend. Both satisfy a division/remainder identity using their own quotient rule. Python int has arbitrary precision subject to resources; it is not a fixed-width C# int. Convert input explicitly with int(text).

Transfer mistake

Do not substitute // for C# integer / without checking negative operands. float is binary floating point, not C# decimal; keep the course money values in integer cents.

Your task

Read an integer numerator and a positive integer divisor, one per line. Convert both from text. Output Python true division, then floor division, on separate lines using default numeric printing. Numerators lie in [-100, 100]; the divisor is 1, 2, or 4. These cases have exact binary quotients. Negative numerators distinguish Python flooring from C# truncation.

challenge icon

Challenge

Beginner

Read an integer numerator and a positive integer divisor, one per line. Convert both from text. Output Python true division, then floor division, on separate lines using default numeric printing. Numerators lie in [-100, 100]; the divisor is 1, 2, or 4. These cases have exact binary quotients. Negative numerators distinguish Python flooring from C# truncation.

Editable implementation files: main.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.

Use the supplied input contract without prompts. Print only the required lines, each ending in a newline.

Try it yourself

# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module

run_module("main", run_name="__main__")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for C# Developers

Practice on your own: Online Python compiler