Sei sulla pagina 1di 5

National University of Modern

Languages

Assignment # 02

Submitted by Roll No.

Raja Roshaan

Programming Fundamentals
BS (CS) (B)
Shapes
CODE:
#include <iostream>
using namespace std;

void righttriangle() {
int size = 0;
cout << "Enter size: ";
cin >> size;
for (int x = 0; x < size; x++) {
cout << "\t\t";
for (int y = 0; y <= x; y++) {
cout << "*";
}
cout << endl;
}
cout << "\nDone" << endl;
}
void pyramidtriangle() {
int size = 0;
cout << "Enter size: ";
cin >> size;
for (int x = 0; x < size; x++) {
cout << "\t\t";
for (int gap = 0; gap < size-x; gap++) {
cout << " ";
}
for (int y = 0; y < (2 * x - 1); y++) {
cout << "*";
}
cout << endl;
}

cout << "\nDone" << endl;


}
void hollowrectangle() {
cout << "Enter Length: ";
int length = 0;
cin >> length;
cout << "Enter Width: ";
int width;
cin >> width;

for (int x = 1; x <= length; x++) {


cout << "\t\t";
for (int y = 1; y <= width; y++) {

if (x == 1 || x == length) {
cout << "*";
}
if (x > 1 && x < length) {

if (y == 1 || y == width) {
cout << "*";
}
else {
cout << " ";
}
}

}
cout << endl;
}
cout << "\nDone" << endl;
}
void hollowtriangle() {
int size = 0;
cout << "Enter size: ";
cin >> size;
for (int x = 0; x < size; x++) {
cout << "\t\t";
for (int gap = 0; gap < size - x; gap++) {
cout << " ";
}
for (int y = 0; y < (2 * x - 1); y++) {
if (y == 0 || y == 2 * x - 2 || x==size-1) {
cout << "*";
}
else {
cout << " ";
}

}
cout << endl;
}

cout << "\nDone" << endl;


}
void characterC() {
cout << "Enter size: ";
int size;
cin >> size;
for (int x = 0; x < size; x++) {
cout << "\t\t";
for (int y = 0; y < size; y++) {
if (x == 0 || x == size-1 || y == 0) {
cout << "*";
}
else {

}
cout << endl;
}
cout << "\nDone" << endl;
}
void characterX() {
cout << "Enter size: ";
int size;
cin >> size;
for (int x = 0; x < size; x++) {
cout << "\t\t";
for (int y = 0; y < size; y++) {
if (x == y || y==size+1-x-2) {
cout << "*";
}
else {
cout << " ";
}
}
cout << endl;
}
cout << "\nDone" << endl;
}
int main() {
char redo = 'y';
do {

cout << "Select the shape you want to draw:\n1. Right Angle Triangle\n2.
Triangle\n3. Hollow rectangle\n4. Hollow triangle\n5. Character C\n6. Character
X\nEnter your choice: ";
int choice;
cin >> choice;
switch (choice) {

case 1: {
righttriangle();
break;
}

case 2: {
pyramidtriangle();
break;
}

case 3: {
hollowrectangle();
break;
}

case 4: {
hollowtriangle();
break;
}

case 5: {
characterC();
break;
}

case 6: {
characterX();
break;
}

default: {
cout << "Invalid choice" << endl;
break;
}

cout << "Do you want to perform again? Y/n ";


cin >> redo;

}while (redo == 'y' || redo == 'Y');

return 0;

}
OUTPUT:

Potrebbero piacerti anche