Lesson #2. Integers, div and mod operators
Introduction
Div operation is an integer division, where the result is rounded.
The div
operation calculates the integer part of the result of dividing integers (partial qúotient ). For example:
10 div 2 = 5
22 div 7 = 3
65 div 10 = 6
The result of
N div K
shows how many times K «fits inside» N.
The mod
operation calculates the remáinder from dividing one integer by another. In this case, if the numbers are divided évenly the remainder is zero. Example:
10 mod 2 = 0
22 mod 7 = 1
65 mod 10 = 5
The result of
N mod K
shows that "remains of N" after the maximum number of pieces of size K is "thrown" out of it.
By parsing any integer N
into two components - the quotient d
and the remainder m
, using the same divisor K
and operations div
and mod
, you will then easily restore this number by the formula:
Examples
10 div 2 = 5, 10 mod 2 = 0 => 10 = 5*2 + 0
22 div 7 = 3, 22 mod 7 = 1 => 22 = 3*7 + 1
65 div 10 = 6, 65 mod 10 = 5 => 65 = 6*10 + 5
Stándard Form Of A Number
Any number can be disassembled into digits using powers of 10 and specified operations. This rule is called the stándard form of a number:
123 = 1*100 + 2*10 + 3
e.g. So to make some digit to be hundreds you need to myltiple it by 100 (for digit 2 we have 2 * 100 = 200).
3 Rules To Get Digits Of Three-Digit Integer
It can be seen that:- to get hundreds (first digit) of three-digit numbers you need to calculate the quotient of division this number by 100 (
number div 100
: 123 div 100 = 1) - to get tens (second digit) you need to calculate the remainder of the division this number by 100, and then - the quotient of division the result by 10 (1.
number mod 100
: 123 mod 100 = 23; 2.23 div 10
= 2) (also there is another way) - to get units (third digit), you need to calculate remainder of dividing this number by 10 (
number mod 10
: 123 mod 10 = 3).
For numbers of other bit width, these algorithms can change.
Tasks (div and mod)
Follow the rules:
- Save your files with the names of the task (e.g. task-04.pas)
- Give meaning names to your variables
- Use the comments to make the program clear for user
- Give the task of the program in the form of a comment before the program code. For comments use curly brackets:
- Give the results of your program (log) in the form of a comment after the program code. It’s easy to do by simply copying. For comments use curly brackets:
[task-001.pas]
A distance L
in centimeters is known. Use the operation of integer division to find number of meters of the distance (1 meter = 100 centimeters). Use the comments to make the program clear for user. Give the log of your program in the form of a comment after the program code.
[task-002.pas]
A mass in kilo is known. Use the operation of integer division to find the number of ton (1 ton = 1000 kilo). Use the comments to make the program clear for user. Give the log of your program in the form of a comment after the program code.
1. [task-01.pas]
A person's age specified in months is given. Determin a person's age in years (e.g: 65 months is 5 full years, 24 months is 2 years). Check the correctness of your program, give the log in the form of a comment.
2. [task-02.pas]
A two-digit integer is known. Output its right and left digits séparated by commas.
-35 >>> 5, 3 90 >>> 0, 9
Note. Run the program and enter a non-two-digit number. What has happened? Is the result right? Later we will learn how to perform validation of the input data.
3. [task-03.pas]
A two-digit integer is known. Output the addition and the multiplacation of its digits. Check the correctness of your program, give the log in the form of a comment.
35 >>> 8, 15 90 >>> 9, 0 -11 >>> 2, 1
Note. Each digit of the number will be needed twice: in the calculation of the sum — the first time and in the calculation of the multiplacation — the second time. It is recommended to use auxíliary variables for storing the values of the digits.
4. [task-04.pas]
A three-digit integer is known. Output all of its digits (the order does not matter). Check the correctness of your program, give the log in the form of a comment.
-105 >>> 5, 0, 1 or -105 >>> 1, 0, 5
5. [task-05.pas]
A three-digit integer is known. Output the addition of its digits. Make sure that your program works correctly with negative numbers.
6. [task-06.pas]
Two digits from 0 up to 9 are given. Use the standard form of a number to make a number, the digits of which are the specified digits.
3, 5 >>> 35 7, 0 >>> 70 0, 4 >>> 4
7. [task-07.pas]
A two-digit integer is known. Swap the digits of tens and units (left and right digits) in this number. Check the correctness of your program, give the log in the form of a comment.
35 >>> 53 -10 >>> -1
8. [task-08.pas]
A three-digit integer is known. Swap the digits of hundreds and tens in this number (left and middle digits). Check the correctness of your program, give the log in the form of a comment.
9. [task-09.pas]
A three-digit integer is known. Perform a cýclic shift of digits to the left (the digit of units becomes the digit of tens, the digit of tens becomes the digit of hundreds and the digit of hundreds becomes the digit of units).
123 >>> 231 -602 >>> -26
10. [task-10.pas]
A three-digit integer is known. Perform a cýclic shift of digits to the right .