Write a program in C++ to generate the following pyramid of numbers.
0
1 0 1
2 1 0 1 2
3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4
5 4 3 2 1 0 1 2 3 4 5
6 5 4 3 2 1 0 1 2 3 4 5 6
#include <iostream>
int main() {
int n;
std::cout << "Enter a number: ";
std::cin >> n;
for (int i = 0; i <= n; ++i) {
for (int j = i; j >= 0; --j) {
std::cout << j << ' ';
}
for (int j = 1; j <= i; ++j) {
std::cout << j;
if (j != i) std::cout << ' ';
}
std::cout << '\n';
}
return 0;
}
Comments
Leave a comment