Number Truncation Pattern
Introduction
This program illustrates how to generate a pattern by truncating digits from a given number. The user inputs a number, and the program displays a sequence where each line presents the number with its last digit progressively removed. This is useful for understanding digit manipulation and how numbers can be decomposed sequentially. Key steps in the program:
- Read a number from the user.
- Determine the largest power of 10 that is less than or equal to the number.
- Print the number in a pattern by successively removing the last digit and displaying the result on each line.
Code Breakdown
Algorithm
-
Start
-
Initialize Variables:
n
(user input number)l
(to store the largest power of 10 less than or equal ton
)t
(temporary variable for processing the number)
-
Input the Number:
- Prompt the user to enter a number and store it in
n
.
- Prompt the user to enter a number and store it in
-
Calculate the Largest Power of 10:
- Set
l
to 1. - While
t
(initially set ton
divided by 10) is not 0:- Update
l
by multiplying it by 10. - Update
t
by dividing it by 10.
- Update
- Set
-
Print the Pattern:
- Set
t
ton
. - While
t
is not 0:- Print the current value of
t
. - Update
t
by takingt
modulol
. - Update
l
by dividing it by 10.
- Print the current value of
- Set
-
End
Code Explanation
Example Flowchart
Start
|
V
Input the number `n`
|
V
Initialize `l` to 1 and `t` to `n`
|
V
Calculate the largest power of 10 less than or equal to `n`
(While `t` != 0: `t = n / 10` and `l = l * 10`)
|
V
Print "The pattern"
|
V
+------------------------------+
| While `t` != 0: |
| +------------------------+ |
| | Print `t` | |
| | Update `t` to `t % l` | |
| | Update `l` to `l / 10` | |
| +------------------------+ |
+------------------------------+
|
V
End