Direction: Make a C++ program using looping for the following output:
1. Program to print pyramid using numbers.
#include <iostream>
using namespace std;
int main()
{
int rows=5, count = 0, count1 = 0, k = 0;
for(int i = 1; i <= rows; ++i)
{
for(int space = 1; space <= rows-i; ++space)
{
cout << " ";
++count;
}
while(k != 2*i-1)
{
if (count <= rows-1)
{
cout << i+k << " ";
++count;
}
else
{
++count1;
cout << i+k-2*count1 << " ";
}
++k;
}
count1 = count = k = 0;
cout << endl;
}
return 0;
}
2. Inverted full pyramid using # character
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
for(int i = rows; i >= 1; --i)
{
for(int space = 0; space < rows-i; ++space)
cout << " ";
for(int j = i; j <= 2*i-1; ++j)
cout << "# ";
for(int j = 0; j < i-1; ++j)
cout << "# ";
cout << endl;
}
return 0;
}
3. A program that will allow the user to enter the number of rows and
the symbol or character you want, then the program will display
the diamond pattern with the given symbol.
#include<iostream>
using namespace std;
int main()
{
int i, j, rowNum, space;
char symbol;
cout<<"Enter the number of rows: ";
cin>>rowNum;
cout<<"Enter the symbol or character: ";
cin>>symbol;
space = rowNum-1;
for(i=1; i<=rowNum; i++)
{
for(j=1; j<=space; j++)
cout<<" ";
space--;
for(j=1; j<=(2*i-1); j++)
cout<<symbol;
cout<<endl;
}
space = 1;
for(i=1; i<=(rowNum-1); i++)
{
for(j=1; j<=space; j++)
cout<<" ";
space++;
for(j=1; j<=(2*(rowNum-i)-1); j++)
cout<<"*";
cout<<endl;
}
cout<<endl;
return 0;
}