Examples on nested loop
Examples on nested loop
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;
}
5. 1
22
333
4444
55555
Solution
#include <iostream>
void main ()
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 <<'$';
9.
*
**
***
****
*****
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=1; row<=5; row++)
{
for (int space=row; space<5; space++)
cout <<' ';
11.
*
**
***
****
*****
Solution
#include <iostream>
using namespace std;
void main ()
{
for (int row=1; row<=5; row++)
{
for (int space=5; space>row; space--)
cout << ' ';
12.
*****
****
***
**
*
Solution
#include <iostream>
using namespace std;
void main ()
{
#include <iostream>
void main ()
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>
void main ()
int x, sum;
float avg;
sum = 0;
cin>> x;
sum+=x;
avg = sum/3;