Nested Loops
Question 1: Write C++ programs that generates the following shapes:
1. **********
**********
**********
**********
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int i=1; i<=4; i++)
{
for (int j=1; j<=10; j++)
cout << '*';
cout << endl;
}
}
2. *
**
***
****
*****
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=1; row<=5; row++)
{
for (int star=1; star<=row; star++)
cout << '*';
cout << endl;
}
}
3. *****
****
***
**
*
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=5; row>=1; row--)
{
for (int star=1; star<=row; star++)
cout << '*';
cout << endl;
}
}
4. *
**
***
****
*****
****
***
**
*
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=1; row<=4; row++)
{
for (int star=1; star<=row; star++)
cout << '*';
cout << endl;
}
for ( row=5; row>=1; row--)
{
for (int star=1; star<=row; star++)
cout << '*';
cout << endl;
}
}
5. 1
22
333
4444
55555
Solution
#include <iostream>
using namespace std;
void main ()
for (int row=1; row<=5; row++)
for (int star=1; star<=row; star++)
cout << row;
cout << endl;
6. 55555
4444
333
22
1
#include <iostream>
using namespace std;
void main ()
{
for (int row=5; row>=1; row--)
{
for (int star=1; star<=row; star++)
cout << row;
cout << endl;
}
7. 1
12
123
1234
12345
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=1; row<=5; row++)
{
for (int num=1; num<=row; num++)
cout << num;
cout << endl;
}
8. $$$$*
$$$**
$$***
$****
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=4; row>=1; row--)
{
for (int d=1; d<=row; d++)
cout <<'$';
for (int star=4; star>=row; star--)
cout << '*';
cout << endl;
}
9.
*
**
***
****
*****
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=1; row<=5; row++)
{
for (int space=row; space<5; space++)
cout <<' ';
for (int star=1; star<=row; star++)
cout << '*';
cout << endl;
}
}
10.
*****
****
***
**
*
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=5; row>=1; row--)
{
for (int space=5; space>row; space--)
cout << ' ';
for (int star=row; star>=1; star--)
cout <<'*';
cout << endl;
}
11.
*
**
***
****
*****
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=1; row<=5; row++)
{
for (int space=5; space>row; space--)
cout << ' ';
for (int star=row; star>=1; star--)
cout <<'*'<<' ';
cout << endl;
}
12.
*****
****
***
**
*
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=5; row >=1; row--)
{
for (int space = 1; space <=5-row; space++)
cout << ' ';
for (int star = 1; star <=row; star++)
cout << '*'<<' ';
cout << endl;
}
}
13.
*
**
***
****
*****
****
***
**
*
Solution
#include <iostream>
using namespace std;
void main ()
for (int row=1; row<=4; row++)
for (int space=5; space>row; space--)
cout << ' ';
for (int star=row; star>=1; star--)
cout <<'*'<<' ';
cout << endl;
for (row=5; row >=1; row--)
for (int space = 1; space <=5-row; space++)
cout << ' ';
for (int star = 1; star <=row; star++)
cout << '*'<<' ';
cout << endl;
}
Question 2: Write a C++ program that passes through 20 students, and enter 3 marks
for each of them and prints out the average.
Solution
#include <iostream>
using namespace std;
void main ()
int x, sum;
float avg;
for (int s=1; s<=20; s++)
cout<<"enter 3 marks for student no "<<s<<endl;
sum = 0;
for (int mark=1; mark<=3; mark++)
cin>> x;
sum+=x;
avg = sum/3;
cout <<"Average of student no "<<s<<" is "<<avg<<endl;