diff options
solved ex 8 and 9 from sem. 3
2 files changed, 41 insertions, 0 deletions
| -rw-r--r-- | Sem. 03/exercise 8 - dollar pyramid.cpp | 17 | ||||
| -rw-r--r-- | Sem. 03/exercise 9 - rightmost pyramid.cpp | 24 |
diff --git a/Sem. 03/exercise 8 - dollar pyramid.cpp b/Sem. 03/exercise 8 - dollar pyramid.cpp new file mode 100644 index 0000000..e32c5ba --- /dev/null +++ b/Sem. 03/exercise 8 - dollar pyramid.cpp @@ -0,0 +1,17 @@ +#include <iostream> +using namespace std; + +void printPyramid(int n) { + for(int i = 1; i <= n; i++) { + for(int j = 1; j <= i; j++) + cout << "$" << " "; // print as many dollars as the number of the current row + cout << endl; + } +} + +int main() { + int n; + cin >> n; + printPyramid(n); + return 0; +}
\ No newline at end of file diff --git a/Sem. 03/exercise 9 - rightmost pyramid.cpp b/Sem. 03/exercise 9 - rightmost pyramid.cpp new file mode 100644 index 0000000..f966fa2 --- /dev/null +++ b/Sem. 03/exercise 9 - rightmost pyramid.cpp @@ -0,0 +1,24 @@ +#include <iostream> +using namespace std; + +void printRightmostPyramid(int n) { + // print the first part of the pyramid (the one going from 1 to n(n-1)...21) + for(int i = 1; i <= n; i++) { + for(int k = n - i; k >= 0; k--) cout << " "; // print the spaces + for(int j = i; j >= 1; j--) cout << j; // print numbers j(j - 1)...21 + cout << endl; + } + // print the first part of the pyramid (the one going from (n-1)...21 to 1 + for(int i = n - 1; i >= 1; i--) { + for(int k = n - i; k >= 0; k--) cout << " "; + for(int j = i; j >= 1; j--) cout << j; + cout << endl; + } +} + +int main() { + int n; + cin >> n; + printRightmostPyramid(n); + return 0; +}
\ No newline at end of file |
