Lab 7
Lab 7
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;
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 {
}while (counter<=100);