0% found this document useful (0 votes)
22 views

Direction: Make A C++ Program Using Looping For The Following Output

The document contains 3 C++ programs that use loops to print different patterns: 1. A pyramid of numbers using nested for loops. 2. An inverted full pyramid using # characters, with for loops to control the rows and columns. 3. A program that takes user input for the number of rows and symbol, then uses for loops to print a diamond pattern with the given symbol.

Uploaded by

Marvin Cinco
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Direction: Make A C++ Program Using Looping For The Following Output

The document contains 3 C++ programs that use loops to print different patterns: 1. A pyramid of numbers using nested for loops. 2. An inverted full pyramid using # characters, with for loops to control the rows and columns. 3. A program that takes user input for the number of rows and symbol, then uses for loops to print a diamond pattern with the given symbol.

Uploaded by

Marvin Cinco
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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;
}

You might also like