C++ bootcamp · Lab 24

Countdown

easyFor Loop8 minLesson: For Loop

Read the question, write C++ on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read n and print the numbers from n down to 1, space-separated on one line.

Input. One line: an integer n ≥ 1.

Output. n space-separated integers, descending.

Constraints

  • 1 ≤ n ≤ 50

Examples

Example 1
Input
5
Output
5 4 3 2 1
Hint
  1. for (int i = n; i >= 1; i--) std::cout << i << " ";
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <iostream>

int main() {
  int n;
  std::cin >> n;
  for (int i = n; i >= 1; i--) std::cout << i << " ";
  std::cout << "\n";
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.