Answer to Question #330000 in C++ for milan

Question #330000

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

1
Expert's answer
2022-04-18T08:40:30-0400
#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;
}

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