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); // 3Python
print(-7 // 3) # -3
print(-7 % 3) # 2
print(7 / 2) # 3.5What 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
BeginnerRead 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__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for C# Developers
1C# Foundations in Python
Run the Python WorkspaceOutput, Strings, and CommentsTyped Locals to Python NamesDivision and RemaindersIndentation Instead of BracesRecap: Port a Receipt4Loops and Transformations
From foreach to forIndexes and Dictionary ItemsSentinels and Loop ControlFrom LINQ to ComprehensionsRecap: Stable Ranking2None and Decisions
Nullable Values and NoneEquality, Identity, and TypesTruth Tests and FallbacksBranch PriorityRecap: Eligibility Rules3Collections and References
From List<T> to listTuples and UnpackingDictionary Keys and ValuesSets, Aliases, and CopiesRecap: Stock Lookup6Reliable Python Programs
Input and Exact OutputExceptions and ValidationImports and the Main GuardType Hints and Runtime TypesRecap: Validate a Batch9Project: Order Processor
Project 1: Order ModelProject 2: Pricing PoliciesPractice on your own: Online Python compiler