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

nested_loop_aloba

Uploaded by

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

nested_loop_aloba

Uploaded by

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

Technological Institute of the Philippines – Manila

CITE 002 – Computer Programming 1

Name GRACCO YANCY ORVEN ALOBA


Section CS11S1

Instructions
1. Do not change the format of the template.
2. Submit your document as “nested_loop_lastname.pdf”.
3. Put answers on items in RED.

Intended Learning Objectives


At the end of the laboratory activity, the student must be able to:

1. Implement nested loops in solving the problem.


2. Analyze a problem if it can be solved via simple or nested loop.
3. Generate a shape using looping statements.

Activity
Create a program that will ask the user to input a number and displays a number of
triangles, square of ‘*’, and a diamond of ‘*’.

Note: Your display does not need to be in a table as long as it displays the three shapes
based on user input.

Example 1:

Input a number: 3

Number of triangle Square of ‘*’ Diamond of ‘*’


1 *** *
12 *** ***
123 *** *****

Example 2:

Input a number: 5
Technological Institute of the Philippines – Manila
CITE 002 – Computer Programming 1
Number of triangle Square of ‘*’ Diamond of ‘*’
1 ***** *
12 ***** ***
123 ***** *****
1234 ***** *******
12345 ***** *********

Evidences/Answer
1. Run the program and input a random number between 5 – 10. Take a screenshot
and put it below.
Technological Institute of the Philippines – Manila
CITE 002 – Computer Programming 1

2. Run the program and input a random number between 5 – 10. Take a screenshot
and put it below.
Technological Institute of the Philippines – Manila
CITE 002 – Computer Programming 1

3. Source code. Put your raw source code below. Not a screenshot.

#include <iostream>
using namespace std;

int main()
{
cout << "Input a number: ";
int num; cin >> num;

//number triangle
cout << endl << "Number of triangle" << endl;
for (int i = 1; i <= num; i++){
for (int j = 1; j <= i; j++) cout << j;
cout << endl;
}

cout << endl << "Square of *" << endl;


//square asterisk
for (int i = 0; i < num; i++){
for (int j = 0; j < num; j++) cout << '*';
cout << endl;
}

cout << endl << "Diamond of *" << endl;


//asterisk pyramid
for (int i = 1; i <= num; i++){
for (int j = 1; j <= (num * 2 - 1); j++){
if (j <= num - i || j >= num + i) cout << ' ';
else cout << '*';
}
cout << endl;
}

return 0;
}

You might also like