Nested Loops
Question 1: Write C++ programs that generates the following shapes:
- **********
**********
**********
**********
Solution
#include <iostream
using namespace std;
void main ()
{
for (inti=1; i<=4; i++)
{
for (int j=1; j<=10; j++)
cout < '*';
coutendl;
}
}
- *
**
***
****
*****
Solution
#include <iostream
using namespace std;
void main ()
{
for (int row=1; row<=5; row++)
{
for (int star=1; star<=row; star++)
cout < '*';
coutendl;
}
}
- *****
****
***
**
*
Solution
#include <iostream
using namespace std;
void main ()
{
for (int row=5; row>=1; row--)
{
for (int star=1; star<=row; star++)
cout < '*';
coutendl;
}
}
- *
**
***
****
*****
****
***
**
*
Solution
#include <iostream
using namespace std;
void main ()
{
for (int row=1; row<=4; row++)
{
for (int star=1; star<=row; star++)
cout < '*';
coutendl;
}
for ( row=5; row>=1; row--)
{
for (int star=1; star<=row; star++)
cout < '*';
coutendl;
}
}
- 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;
coutendl;
}
}
- 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;
coutendl;
}
}
- 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;
coutendl;
}
}
- $$$$*
$$$**
$$***
$****
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 < '*';
coutendl;
}
}
*
**
***
****
*****
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 < '*';
coutendl;
}
}
*****
****
***
**
*
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 <'*';
coutendl;
}
}
*
* *
* * *
* * * *
* * * * *
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 <'*'<' ';
coutendl;
}
}
* * * * *
* * * *
* * *
* *
*
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 < '*'<' ';
coutendl;
}
}
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
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 <'*'<' ';
coutendl;
}
for (row=5; row >=1; row--)
{
for (int space = 1; space <=5-row; space++)
cout < ' ';
for (int star = 1; star <=row; star++)
cout < '*'<' ';
coutendl;
}
}
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;
floatavg;
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 "<avgendl;
}
}