191k views
25 votes
Using a while loop, create an algorithm extractDigits that prints the individual digits of a positive integer.

User Carltonp
by
4.8k points

2 Answers

5 votes

Final answer:

The question involves writing an algorithm using a while loop to extract and print the digits of a positive integer from least significant to most significant, which is associated with high school level Computers and Technology.

Step-by-step explanation:

The question requires an algorithm that uses a while loop to separates the digits of a given positive integer. Here's a simple algorithm for extracting and printing each digit of a positive integer:

  • Start with the positive integer that you want to extract digits from.
  • Use a while loop to continue the process as long as the number is greater than 0.
  • Inside the loop, use the modulus operator (%) to get the last digit of the number.
  • Print the digit obtained from the modulus operation.
  • Divide the number by 10 to remove the last digit.
  • Repeat the loop until the number is reduced to 0.
  • In pseudocode, your algorithm
  • extractDigits
  • might look something like this:

function extractDigits(number):
while number > 0:
digit = number % 10
print(digit)
number = number // 10
end function

This algorithm prints each digit starting from the least significant digit (the ones place) to the most significant digit (the highest place value).

User Vidish Purohit
by
5.5k points
3 votes

Answer:

The algorithm is as follows

1. Start

2. Declare Integer N

3. Input N

4. While N > 0:

4.1 Print(N%10)

4.2 N = N/10

5. Stop

Step-by-step explanation:

This line starts the algorithm

1. Start

This declares an integer variable

2. Declare Integer N

Here, the program gets user input N

3. Input N

The while iteration begins here and it is repeated as long as N is greater than 0

4. While N > 0:

This calculates and prints N modulus 10; Modulus of 10 gets the individual digit of the input number

4.1 Print(N%10)

This remove the printed digit

4.2 N = N/10

The algorithm ends here

5. Stop

Bonus:

The algorithm in Python is as follows:

n = 102

while n>0:

print(int(n%10))

n= int(n/10)

User Alex Stoyanov
by
5.3k points