2 ask 3 Print the following combination of stars using a for

2.

ask 3: Print the following combination of stars using a for loop nested inside a for loop. Then a while loop nested inside a for loop. You are welcome to use as many nested loops as it takes to solve the problem.

Each letter will have two separate programs that produce the same output.

Attach Snipping photo of the source code for all 6 exercises and output.

A.

*****

****

***

**

*


B.

         *

       **

     ***

   ****

*****






C.

**********

********

   ******

     ****

       **

Solution

No programming language specified, hence writing it in C.

#include <stdio.h>

int main(void) {
   // your code goes here
  
   //------------------------------------------------------------------------------------------
   //Answer for A
   //Using for inside for
   int i, j;
   int k;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf(\"*\");
}
printf(\"\ \");
}
//Using while inside for
  
for(i=5;i>=1;i--)
{
j=1;
while(j<=i)
{
printf(\"*\");
j++;
}
printf(\"\ \");
}
//----------------------------------------------------------------------------------------------
//Answer for B
//Using for inside for
for(i=5;i>=1;i--)
{
for(j=1;j<i;j++)
{
printf(\" \");
}
for(k=5;k>=i;k--)
{
printf(\"*\");
}
printf(\"\ \");
}
  
//Usign while inside for
  
for(i=5;i>=1;i--)
{
k=5;
j=1;
while(j<i)
{
printf(\" \");
j++;
}
while(k>=i)
{
printf(\"*\");
k--;
}
printf(\"\ \");
}

   //---------------------------------------------------------------------------------------------
   //Answer for C
   //Using for inside for
   for(i=5;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(\" \");
}
for(k=1;k<(i*2);k++)
{
printf(\"*\");
}
printf(\"\ \");
}
  
//Using while inside for
  
for(i=5;i>=1;i--)
{
j=5;
k=1;
while(j>i)
{
printf(\" \");
j--;
}
while(k<(i*2))
{
printf(\"*\");
k++;
}
printf(\"\ \");
}
  
  
   return 0;
}

2. ask 3: Print the following combination of stars using a for loop nested inside a for loop. Then a while loop nested inside a for loop. You are welcome to use
2. ask 3: Print the following combination of stars using a for loop nested inside a for loop. Then a while loop nested inside a for loop. You are welcome to use
2. ask 3: Print the following combination of stars using a for loop nested inside a for loop. Then a while loop nested inside a for loop. You are welcome to use

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site