0% found this document useful (0 votes)
5 views18 pages

Lab 7

Uploaded by

bdra88711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Lab 7

Uploaded by

bdra88711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Lab 7: Nested Loop

Name of the experiment: Implementing C++ nested repetition statements


(C++ program to print a given pattern)

Objective of the experiment: Familiarize student with the usage of:


a) Nested Repetition statements
b) ++ (increment) and -- (decrement) operators with in repetition
statements.

Software/Tool/Parameters required: Computer with Windows 7/8, Dev


C++ compiler.

Program/ Procedure:
1. Implement a C++ Program to print the following patterns:
PART i) PART ii) PART iii) PART iv) PART v)
**** * 1 ***** 55555
**** *** 22 **** 4444
**** **** 333 *** 333
**** ***** 4444 ** 22
**** ****** 55555 * 1
Lab 7(i): C++ Source Code:

#include <iostream>
using namespace std;

int main()
{
int a=5;

for(int i=0;i<a;i++)
{
for(int j=0;j<a;j++){

cout<<"*";

}
cout<<endl;

}
Lab 7(i): Output:

*****
*****
*****
*****
*****

--------------------------------
Process exited after 0.1351 seconds with return value 0
Press any key to continue . . .
Lab 7(ii): C++ Source Code:

#include <iostream>
using namespace std;

int main()
{
int a=6;

for(int i=1;i<=a;i++)
{
for(int j=1;j<=i;j++){

cout<<"*";

}
cout<<endl;

}
Lab 7(ii): Output:
*
**
***
****
*****
******

--------------------------------
Process exited after 0.1441 seconds with return value 0
Press any key to continue . . .
Lab 7(iii): C++ Source Code:

#include <iostream>
using namespace std;

int main()
{
int a=5;

for(int i=1;i<=a;i++)
{
for(int j=1;j<=i;j++){

cout<< i;

}
cout<<endl;

}
Lab 7(iii): Output:
1
22
333
4444
55555

--------------------------------
Process exited after 0.143 seconds with return value 0
Press any key to continue . . .
Lab 7(iv): C++ Source Code:
#include <iostream>
using namespace std;

int main()
{
int a=5;

for(int i=a;i>=1;i--)
{
for(int j=1;j<=i;j++){

cout<<"*";

}
cout<<endl;

}
Lab 7(iv): Output:

*****
****
***
**
*

--------------------------------
Process exited after 0.1206 seconds with return value 0
Press any key to continue . . .
Lab 7(v): C++ Source Code:

#include <iostream>
using namespace std;

int main()
{
int a=5;

for(int i=a;i>=1;i--)
{
for(int j=1;j<=i;j++){

cout<< i;

}
cout<<endl;

}
Lab 7(v): Output:

55555
4444
333
22
1

--------------------------------
Process exited after 0.1555 seconds with return value 0
Press any key to continue . . .
2. Find the error(s) in each of the following codes, rewrite the correct
code:
a) The following code should output the integers from 100 to 1:
For (int x = 100, x >= 1, ++x )
cout << x << endl;

b) The following code should output the odd integers from 19 to 1:


for (int x = 19; x >= 1; x += 2 )
cout << x << endl;

c) The following code should output the even integers from 2 to


100:
int counter = 2;
do
{
cout >> counter >> endl;
counter += 2;
} While ( counter > 100 )

A- #include <iostream>
using namespace std;
int main()
{

for(int i=100;i>=1;i--){

cout<<i<<endl;
}

B- #include <iostream>
using namespace std;

int main()
{

for(int i=19;i>=1;i-=2){

cout<<i<<endl;
}

C-#include <iostream>
using namespace std;
int main()
{
int counter=2;

do {

cout<< counter <<endl;


counter+=2;

}while (counter<=100);

You might also like