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
#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;
}
Comments
Leave a comment