Lesson #12. While & Repeat Loops: digits of a number
Sum of digits of natural number
Task: Natural number m
is given. Calculate the sum of its digits.
m=425 result: 11
********************************************************************************************
✎
1. {0.3 points}[L12-task-01.pas]
An integer is given (variable a
). Find the number of its digits and their sum.
For example:
Example1: N = 1205 >>> Count = 4, Sum = 8 Example2: N = -111 >>> Count = 3, Sum = 3
*******************************************************************************************************
Quantity of natural number digits satisfying some condition
Task: Natural number m
is given. How many "1" digits in its decimal representation does it have?
m=4121 result: 2
*******************************************************************************************
✎2. {0.3 points}[L12-task-02.pas]
Natural number m
is given. Calculate the sum of its odd digits.
For example:
m = 136 result: 2
3. {0.3 points}[L12-task-03.pas]
Natural number m
is given. Calculate the sum of its digits that are divisible by 3.
For example:
Example1: N = 1309 >>> digits: 3, 9; their sum: 12 Example2: N = -123 >>> digits: 3; their sum: 3 Example3: N = -623 >>> digits: 6, 3; their sum: 9
4. {0.4 points}[L12-task-04.pas]
An integer is given. Output its digits, starting with the last digit (from right to left).
For example:
Example1: N = 1205 >>> 5, 0, 2, 1 Example2: N = -123 >>> 3, 2, 1 Example3: N = -111 >>> 1, 1, 1
5. {0.4 points}[L12-task-05.pas]
A sequence of 4 digits is given. Form a positive number consisting of these digits, starting with the last digit (from right to left).
For example:
Example1: 3, 7, 0, 2 >>> number = 2073 Example2: 2, 1, 4, 3 >>> number = 3412
6. {0.6 points}[L12-task-06.pas]
A natural number N
which is the quantity of the digits of some number is given; and also the digits themselves are given. Form a positive number consisting of these digits.
For example:
Example1: N = 3; digits = 3,7,0 >>> number = 370 Example2: N = 5; digits = 0,7,0,1,6 >>> number = 7016 Example3: N = 1; digits = 2 >>> number = 2 Example4: N = 2; digits = 0,2 >>> number = 2
7. {0.6 points}[L12-task-07.pas]
An integer is given. Output the value of the largest digit of this number.
For example:
Example1: N = 50 >>> MaxDig = 5 Example2: N = 1946 >>> MaxDig = 9 Example3: N = -13 >>> MaxDig = 3
8. {1.0 points}[L12-task-08.pas]
An integer is given. Output the value of the lowest digit of this number and also output its position. Try to use only one loop in the solution.
For example:
Example1: N = -50 >>> MinDig = 0, Pos = 2 Example2: N = 13 >>> MinDig = 1, Pos = 1 Example3: N = 8287 >>> MinDig = 2, Pos = 2
9. {0.7 points}[L12-task-9.pas]
Two integers are given. Form a new number which begins with the digits of the first integer and finishes with the digits of the second integer.
For example:
Example1: N1 = 1, N2 = 390 >>> number = 1390 Example2: N1 = 10, N2 = 21 >>> number = 1021 Example3: N1 = 72, N2 = 2 >>> number = 722*********************************************************************
The problem of finding a value
Task. N
numbers are given. Is there a number k
among them?
Solution. Let's define logical variable Exists
, Initialized by False
value in the beginning. Then In a loop we will change it into "True"
value if there is any number equals k
.
println('please, input a number to find'); var k:=readInteger;println(Exists);
Example1: please, input a number to find k = 6 n = 3 x: 12 7 6 exists = true Example2: please, input a number to find k = 5 n = 4 x: 12 7 6 8 exists = false
10. {0.5 points}[L12-task-10.pas]
An integer N
and a digit D
are given. Output True
if there is the digit D
in the number N
, and output False
otherwise.
For example:
Example1: N = 291, D = 9 >>> True (there is digit 9 in the number 291) Example2: N = 1234, D = 9 >>> False (there is no digit 9 in the number 1234)****************************************************************
Break & Continue statements
When a program executes a break
statement which is inside a loop, it stops immediately and the program runs the next statement after the loop.
When a program executes a continue
statement which is inside a loop, the current iteration of the loop is immediately terminated and the
program runs the next iteration of the loop.
break
и continue
statements can be used only in loops
The problem of finding a value with break
Task. N
numbers are given. Is there a number k
among them?
Solution. Let's define logical variable Exists
, Initialized by "False" value in the beginning. Then In a loop we will change it into "True"
value if there is any number equals k
. And If the value is found, we break the loop
var k := readInteger;***************************************************************
✎
11. {0.5 points}[L12-task-11.pas]
An integer N
and a digit D
are given. If there is the digit D
in the number N
, output True
, otherwise output False
. Use a break
statement.
For example:
Example1: N = 291, D = 9 >>> True (there is digit 9 in the number 291) Example2: N = 1234, D = 9 >>> False (there is no digit 9 in the number 1234)
12. {0.5 points}[L12-task-12.pas]
An integer N
is given. If there is even digit among the digits of the number N
, output True
, otherwise output False
. Use a break
statement.
For example:
Example1: N = 2914 >>> True Example2: N = 1931 >>> False