Lesson #2. Working with digits of a number

Theory

Lecture # 2 in pdf format

Integer division and remainder after division

    Div
  • 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
    
    The result of 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
  • The 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
    
    The result of 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

    Formula to get the number out of the quotient, divisor and remainder

    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 
    
Standard Form Of A Number

Any number can be disassembled into digits using powers of 10 and specified operations. This rule is called the standard form of a number:

123 = 1*100 + 2*10 + 3

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).

3 Rules To Get Digits Of Three-Digit Integer (three bit width)
    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.

{0.2} Task 1:
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.

Expected output:

Please enter the distance in centimeters
>>> 245
The distance in meters is  2.45 

[Program name: L2task01.pas]

{0.2} Task 2:
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.

Expected output:

Please enter the mass in kilos
>>> 4527
The mass in tons is 4 

[Program name: L2task02.pas]

{0.3} Task 3:
To do: The person’s age in months is entered. 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 as a comment.

Expected output:

Please enter the age in months
>>> 37
The age in years is 3 

[Program name: L2task03.pas]

{0.3} Task 4:
To do: A two-digit integer is entered. Output its first and second digits separated by commas (i.e. left and right digits).

Note. Run the program and enter a non-two-digit number. What has happened? Is the result correct? Later we will learn how to perform validation of the input data.

Expected output:

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]

{0.3} Task 5:
To do: A two-digit integer is entered. Output the addition and multiplication of its digits. Check the correctness of your program.

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 multiplication — the second time. It is recommended to use auxíliary variables for storing the values of the digits.

Expected output:

please enter a two digit number
>>> 73
you entered 73. The result is 10, 21 

[Program name: L2task05.pas]

{0.3} Task 6:
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.

Expected output:

Please enter a three-digit number
>>> -105
The three digits are : 1, 0, 5 

[Program name: L2task06.pas]

{0.3} Task 7:
To do: A three-digit integer is entered. Output the addition of its digits. Make sure that your program works correctly with negative numbers.

Expected output:

Please enter a three-digit number
>>> -745
You entered: 745. The addition of its three digits is equal to 16 

[Program name: L2task07.pas]

{0.4} Task 8:
To do: Two digits (in the range from 0 to 9) are entered. Use the standard form of a number to make a number, the digits of which are the specified digits.

Expected output:

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]

{0.4} Task 9:
To do: A two-digit integer is entered. Swap the values of its left and right digits and output the resulting number. Check the correctness of your program, give the log in the form of a comment.

Expected output:

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]

{0.4} Task 10:
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.

Expected output:

please enter a three digit integer
>>> 846
After swaping the left and middle digits, the result is 486 

[Program name: L2task10.pas]

{0.4} Task 11:
To do: A three-digit integer is entered. Make a cyclic shift of digits to the left.

Expected output:

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]

{0.4} Task 12:
To do: A three-digit integer is known. Make a cyclic shift of digits to the right.

Expected output:

please enter a three digit integer
>>> -184
After a cyclic shift of digits to the right, the result is -418 

[Program name: L2task12.pas]


Tasks for self-solving

Note: tasks should be saved in a file with the name of the task, and be sure to insert a comment with the statement of the task in the code.

INTEGER

  1. Integer7. A two-digit integer is given. Find the sum and the product of its digits.
    Expected output:
    << 47
    results:
    sum = 11  product = 28
    
  2. Integer9. A three-digit integer is given. Using one operator of integer division find first digit of the given integer (a hundreds digit).
    Expected output:
    << 188
    results:
    1
    
  3. Integer10. A three-digit integer is given. Output its last digit (a ones digit) and then its middle digit (a tens digit).
    Expected output:
    << 916
    results:
    ones 6, tens 1
    
  4. Integer11. A three-digit integer is given. Find the sum and the product of its digits.
    Expected output:
    << 363
    results:
    sum 12, product 54
    
  5. Integer12. A three-digit integer is given. Output an integer obtained from the given one by reading it from right to left.
    Expected output:
    << 263
    results:
    362
    
  6. Integer13. A three-digit integer is given. Output an integer obtained from the given one by moving its left digit to the right side.
    Expected output:
    << 124
    results:
    241