Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Introduction to Computing

Lab 09
Topic Loop Statement
Objective Learning how to use repetition statements

do while:

The general form of do while loop is:

Flow of do while loop:

The statement executes first, and then the expression is evaluated. If the expression evaluates to true,
the statement executes again. As long as the expression in a do. . .while statement is true, the statement
executes. To avoid an infinite loop, you must, once again, make sure that the loop body contains a
statement that ultimately makes the expression false and assures that it exits properly. In C++, do is a
reserved word.

Nested Repetition statement:

A repetition statement is defined inside a repetition statement is called nested repetition.


Task 1
Write a C++ program to print a hollow triangle on console using Loop statement as shown
below:

OUTPUT:

Task 2
Write a C++ program to print a filled triangle on console using loop statement as shown below:

OUTPUT:
Task 3
Write a C++ program to print a hollow hexagonal shape on console using loop statement as
shown below:

OUTPUT:

#include<iostream>
using namespace std;
int main()
{
for (int r = 1;r <= 4;r++)
{
for (int c = 1;c <= 6;c++)
{
if (r == 1 && c == 1 || r == 1 && c == 6 || r == 4 && c == 1 || r == 4 && c == 6||
r==2&&c==2||r==2&&c==3||r==2&&c==4||r==2&&c==5||r==3&&c==2||r==3&&c==3||r==3&&c==4||r==3&&c==5)
{
cout << " ";
}
else
{
cout << "*";
}
}cout << endl;
}

}
Task 4
Write a C++ program to print a filled hexagonal shape on console using

loop statement as shown below:

OUTPUT:

#include<iostream>
using namespace std;
int main()
{
for (int r = 1;r <= 4;r++)
{
for (int c = 1;c <= 6;c++)
{
if (r == 1 && c == 1 || r == 1 && c == 6 || r == 4 && c == 1 || r == 4 && c == 6 )
{
cout << " ";
}
else
{
cout << "*";
}
}cout << endl;
}

Task 5
Write a C++ program to print a hollow square shape on console using loop statement as shown
below:

OUTPUT:
Task 6
Write a C++ program to print a filled square shape on console using loop statement as shown
below:

OUPUT:

Task 7

Write a C++ program to print a hollow diamond shape on console using loop statement as
shown below:

OUTPUT:

#include<iostream>
using namespace std;
int main()
{
for (int r = 1;r <= 5;r++)
{
for (int c = 1;c <= 7;c++)
{
if (r == 1 && c == 4 || r == 2 && c == 2 || r == 2 && c == 6 || r == 3 && c == 1 || r == 3 &&
c == 7 || r == 4 && c == 2 || r == 4 && c == 6 || r == 5 && c == 4)
{
cout << "*";
}
else
{
cout <<" ";
}
}cout << endl;
}
}
Task 8
Write a C++ program to print a filled diamond shape on console using loop statement as shown
below:

OUTPUT:

using namespace std;


int main()
{
for (int r = 1;r <= 5;r++)
{
for (int c = 1;c <= 9;c++)
{
if (r == 1 && c == 5 || r == 2 && (c >= 3 && c <= 7) || r==3&&(c>=1&&c<=9) || r == 4 &&
(c >= 3 && c <= 7) || r == 5 && c == 5)
{
cout << "*";
}

else
{
cout << " ";
}
}cout << endl;
}

Task 9
Print Square in the middle of console screen as follows using loop statement as shown below:
OUTPUT:

#include<iostream>
using namespace std;
int main()
{
for (int r = 1;r <= 8;r++)
{
for (int c = 1;c <= 14;c++)
{

if (r==1||c==1||r==8||c==14)
{
cout << "@";
}
else
{
cout << " ";
}
}cout << endl;
}
system("pause");
return 0;
}
Task 10
Print Square in the middle of console screen as follows using loop statement as shown below:

OUTPUT:

#include<iostream>
using namespace std;
int main()
{
for (int r = 1;r <= 6;r++)
{
for (int c = 1;c <= 14;c++)
{
if (r==1||c==1||r==6||c==14||r==2&&(c==4||c==11)||r==3&&(c==3||c==12)||r==4&&(c==2||
c==13))
{
cout << "@";
}

else
{
cout << " ";
}
}cout << endl;
}
}

Task 11

Write a program to print the following shape on the screen


(a) (b)
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******

A)

Task 12

Write a program to print the following shape on the screen.

(b)
(a)
*******
*******
******
******
*****
*****end
****
****
***
***
**
**
*
*
Task 13

Write a program to print the following shape on the screen

(a)
(b)
1
12345
12
1234
123
123
1234
12
12345
1

Task 14

Write a program to print the following shape on the screen.

54321

4321

321

21

Task 15

Write a program to print the following shape on the screen

24

369

4 8 12 16

5 10 15 20 25
Task 16

Write a program to print the following shape on the screen

(a) (b)
1 7777777
22 666666
333 55555
4444 4444
55555 333
666666 22
7777777 1

You might also like