Assignment Sir Amir

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.

Write a program using C++ to implement the given shape


below using loops of your choice.
*
* *
* * *
* * @ @
* * * * *

#include <iostream>
using namespace std;

int main() {
int rows = 5; // Number of rows in the shape

// Outer loop for rows


for (int i = 0; i < rows; i++)
{
// Inner loop for spaces before the stars
for (int j = 0; j < 2 * (rows - i - 1); j++) {
cout << " "; // Two spaces for alignment
}

// Inner loop for le� stars


for (int k = 0; k <= i; k++) {
if (i == 3 && (k == i - 1 || k == i))
{
// 4th row and 2nd last and last posi�ons (index starts from 0)
cout << "@ ";
} else {
cout << "* ";
}
}

cout << endl;

return 0;
}
OUTPUT
2 Write a program using C++ to implement the given shape below
using loops of your choice. Take the screen shot when program
runs successfully and shows the same output as given below.

*
# #
& & &

#include <iostream>
using namespace std;

int main() {
int rows = 3;

for(int i = 1; i <= rows; ++i) {


for(int j = 1; j <= i; ++j) {
if(i == 1) {
cout << "* ";
} else if(i == 2) {
if(j == 1) {
cout << "# ";
} else {
cout << "# ";
}
} else {
cout << "& ";
}
}
cout << "\n";
}

return 0;
}

OUTPUT

You might also like