Содержание:
Theory
Lection # 2 in pdf format
Integer division and remainder after division
- The
div
operation is an integer division, where the result is a number without fractional part. - The
div
operation calculates the integer part of the result of dividing integers (partial quotient ). For example:
10 div 2 = 5 22 div 7 = 3 65 div 10 = 6 --- N div K
N
div K
shows how many times K
«fits inside» N
.Examples:
Example 1.
How many Kilobytes are in x
bytes?
Answer: x div 1024
Example 2.
x
is a time in seconds
How many seconds have passed since the last minute?
Answer: x mod 60
mod
operation calculates the remainder after dividing two integers. In this case, if the numbers are evenly divisible, the remainder is zero. Example:10 mod 2 = 0 22 mod 7 = 1 65 mod 10 = 5 --- N mod K
N
mod K
shows the «remainder of N» after the maximum number of K
is «thrown» out of N
.Examples
Example 3.
x mod 2 = 0 ⟹ x – even number x mod 2 <> 0 ⟹ x – odd number
Example 4.
var x := 1234; var LastDigit := x mod 10; // 4 var NumWithoutLastDigit := x div 10; // 123 |
Example 5.
// x is a 3-digit number. What is the second digit? // Answer: var x := ReadInteger('Enter x:'); // 456 x := x div 10; // 45 Print(x mod 10); // 5 |
Bitwise number operation
By parsing any integer N
into two components — the quotient d
and remainder m
, using the same divisor K
and operations div
and mod
, you will then easily restore this number by the formula:
If we have 10: 10 div 2 = 5 10 mod 2 = 0 ↓ 5 * 2 + 0 = 10
Examples:
10 div 2 = 5; 10 mod 2 = 0 => 5*2 + 0 = 10 22 div 7 = 3; 22 mod 7 = 1 => 3*7 + 1 = 22 65 div 10 = 6; 65 mod 10 = 5 => 6*10 + 5 = 65
Any number can be disassembled into digits using powers of 10 and specified operations. This rule is called the standard form of a number:
Therefore, to make a certain number a hundred, you need to multiply it by 100 (as we have for digit 1
we have 1 * 100
= 100
).
-
It can be seen that:
- to get hundred (first digit) of three-digit number, you need to calculate the quotient after dividing that number by 100 (number div 100, e.g.
123 div 100 = 1
) - to get ten (second digit), you need to calculate the remainder after dividing that number by 100, and then — the quotient of dividing the result by 10 (1. number mod 100:
123 mod 100 = 23;
2.23 div 10 = 2
) (also there is another way) - to get unit number (third digit), you need to calculate remainder after dividing this number by 10 (number mod 10:
123 mod 10 = 3
).
For numbers with a different bit width, these algorithms can be changed.
Labs and Tasks
To complete the tasks, please, follow the rules.
To do: A distance
L
in centimeters is entered. Use the operation of integer division to convert it to meters of (1 meter = 100 centimeters). Use comments to make the program clear to user. Give the log of your program in the form of a comment after the program code.
The resulting example:
Please enter the distance in centimeters >>> 245 The distance in meters is 2.45
[Program name: L2task01.pas]
To do: A mass in kilograms is entered. Use integer division to convert it to tons (1 ton = 1000 kilos). Use comments to make the program understandable to user. Give the log of your program as a comment after the program code.
The resulting example:
Please enter the mass in kilos >>> 4527 The mass in tons is 4
[Program name: L2task02.pas]
To do: You are given the person’s age in months. Convert it to a person’s age in years (e.g: 65 months is 5 full years, 24 months is 2 years). Verify the correctness of your program, give the log s a comment.
The resulting example:
Please enter the age in months >>> 37 The age in years is 3
[Program name: L2task03.pas]
To do: A two-digit integer is known. Output its first and second digits separated by commas.
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.
The resulting example:
Please enter a two digit number >>> 37 The first and second are 7, 3 +++ Please enter a two digit number: >>> -35 The first and second are 5, 3 +++ Please enter a two digit number: >>> 90 The first and second are 0, 9
[Program name: L2task04.pas]
To do: You are given a two-digit integer. Output the addition and multiplacation of its digits. Check the correctness of your program, give the log in the form of a comment.
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.
The resulting example:
please enter a two digit number >>> 73 you entered 73. The result is 10, 21
[Program name: L2task05.pas]
To do: A three-digit integer is entered. 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.
The resulting example:
Please enter a three-digit number >>> -105 The three digits are : 1, 0, 5
[Program name: L2task06.pas]
To do: A three-digit integer is entered. Output the addition of its digits. Make sure that your program works correctly with negative numbers.
The resulting example:
Please enter a three-digit number >>> -745 You entered: 745. The addition of its three digits is equal to 16
[Program name: L2task07.pas]
To do: You are given two digits from 0 to 9. Use the standard form of a number to make a number, the digits of which are the specified digits.
The resulting example:
Please enter two digits >>> 3 >>> 5 The new number is 35
Check the following:
3, 5 => 35 7, 0 => 70 0, 4 => 4
[Program name: L2task08.pas]
To do: A two-digit integer is entered. Swap the values of its left and right digits. Check the correctness of your program, give the log in the form of a comment.
The resulting example:
please enter a two digit number >>> -57 you entered -57. The result is -75
Check the following:
35 => 53 -10 => -1
[Program name: L2task09.pas]
To do: A three-digit integer is entered. Swap the values of its left and middle digits. Check the correctness of your program, give the log in the form of a comment.
The resulting example:
please enter a three digit integer >>> 846 After swaping the left and middle digits, the result is 486
[Program name: L2task10.pas]
To do: A three-digit integer is entered. Make a cyclic shift of digits to the left.
The resulting example:
please enter a three digit integer >>> -734 After making a cyclic shift of digits to the left, the result is -347
Check the following:
123 => 231 -602 => -26
[Program name: L2task11.pas]
To do: A three-digit integer is known. Make a cyclic shift of digits to the right.
The resulting example:
please enter a three digit integer >>> -184 After a cyclic shift of digits to the right, the result is -418
[Program name: L2task12.pas]