Answer to Question #329997 in C++ for milan

Question #329997

Write a program in C++ to read a positive integer number n and to generate the numbers in the

following form.

For example,

Enter a number: 5

Output 5 4 3 2 1 0 1 2 3 4 5

Enter a number: 7

Output 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7


1
Expert's answer
2022-04-18T08:40:32-0400
#include <iostream>

int main() {
  int n;
  std::cout << "Enter a number: ";
  std::cin >> n;
  for (int i = n; i >= 0; --i) {
    std::cout << i << ' ';
  }
  for (int i = 1; i <= n; ++i) {
    std::cout << i;
    if (i != n) std::cout << ' ';
  }
  std::cout << '\n';
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS