Advertisement
Spocoman

01. Number Pyramid

Sep 12th, 2023
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7.     int num;
  8.     cin >> num;
  9.  
  10.     int counter = 1;
  11.  
  12.     for (int row = 1; row <= num; row++) {
  13.         for (int col = 0; col < row; col++) {
  14.             if (counter > num) {
  15.                 break;
  16.             }
  17.             cout << counter << " ";
  18.             counter++;
  19.         }
  20.         if (counter > num) {
  21.             break;
  22.         }
  23.         cout << endl;
  24.     }
  25.  
  26.     return 0;
  27. }
  28.  
  29. Или:
  30.  
  31. #include <iostream>
  32.  
  33. using namespace std;
  34.  
  35. int main() {
  36.  
  37.     int num;
  38.     cin >> num;
  39.  
  40.     int counter = 1;
  41.  
  42.     for (int row = 1; row <= num || counter <= num; row++) {
  43.         for (int col = 0; col < row && counter <= num; col++) {
  44.             cout << counter << " ";
  45.             counter++;
  46.         }
  47.         cout << endl;
  48.     }
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement