217k views
2 votes
suppose n is an integer, write a segment of code using while loop to print out the numbers n, n-1, n-2, until 1 in a single line, seperated by spaces. For example if n is 6, the code will print 6 5 4 3 2 1

User Mohsen TOA
by
8.6k points

1 Answer

1 vote

Answer:

  1. n = 6
  2. output = ""
  3. while(n >=1):
  4. output += str(n) + " "
  5. n = n - 1
  6. print(output)

Step-by-step explanation:

The solution code is written in Python 3.

Firstly, let an initial value 6 to variable n (Line 1).

Next, create a variable output to hold the resulting number string (Line 2).

Create a while loop and set the loop condition to enable the loop keep running so long as n bigger or equal to 1 (Line 3).

In the loop, concatenate the current n value with a single space " " and assign it to output (Line 4)

Next decrement the n number by one before proceeding to next round of loop (Line 5)

After completion of loop, print the output and we shall get 6 5 4 3 2 1

User Abdullah Saleem
by
7.9k points