C++ PinWheel

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

// the pyramid of $ using nested for loops #include <iostream> // replace any occurrences of VIEW with character $ #define

VIEW '$' using namespace std; int main(void) { int i, j; cout<<"Let have a money pyramid!\n"<<endl; // first for loop, set the rows... for(i=1; i<=20; i++) { // second for loop, set the space... for(j=1; j<=20-i; j++) cout<<" "; // third for loop, print the $ characters... for(j=1; j<=2*i-1; j++) // print character cout<<VIEW; // go to new line cout<<"\n"; } System(pause); return 0; }

Output example:
Let have a money pyramid! $ $$$ $$$$$ $$$$$$$ $$$$$$$$$ $$$$$$$$$$$ $$$$$$$$$$$$$ $$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$

#include <iostream> using namespace std; int main() { // declare variables int number=0; int counter=1; int copy=0; // get data from user cout<<"Enter a number: "; cin>>number; // this variable is just a copy // of the number that the user just entered // which will be used in the nested loop copy=number; // this is the start of the nested loop // NOTE: many use two nested 'for' loops, but // sometimes its nice to switch things up while(counter <= number) { for(int x=1; x <= copy; ++x) { cout<<number*x<<" "; } cout<<endl; --copy; ++counter; } return 0; }
Enter a 9 18 27 9 18 27 9 18 27 9 18 27 9 18 27 9 18 27 9 18 27 9 18 9 number: 9 36 45 54 63 72 81 36 45 54 63 72 36 45 54 63 36 45 54 36 45 36

You might also like