Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Wrtie a C program NOT C++ Pattern 3: * *** ***** ******* ********* *********** *

ID: 3834694 • Letter: W

Question

Wrtie a C program NOT C++

Pattern 3:

       *

      ***

     *****

    *******

   *********

***********

*************

Using a nested for loop create patterns 1.2. and 3 shown below. In function main prompt the user for the height (mumber of rows) and width (mumber of characters per line) for pattern i. You may use any number for height and width just as gng as the basic pattern is preserved For pattern 1, make use of an ifstructure to instruct the program what to print on the lines and Also use nested for loops to create patterns 2 and using same height as patt l. i Pattern Pattern 2

Explanation / Answer

Code for Patttern 1

Logic:
It consists of N rows (where N is the total row to be printed). Each row contains exactly N columns.

To iterate through rows, run an outer loop from 1 to N (where N is the total rows to be printed).
To print columns, run an inner loop from 1 to N.
Inside this loop print "=" if i==1 OR i==N (where i is the current row number) else if j==1 OR j==N (where j is the current column number) print "*", otherwise print single space.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
* C program to print hollow square star pattern
*/

#include <stdio.h>

int main()
{
int i, j, N;

printf("Pattern 1: ");

//Reads number of rows from user
printf("Enter total rows : ");
scanf("%d", &N);

//Iterates over each row one by one
   for(int i=1; i<=N; i++)
   {
   //Iterates over each column of the i-th row
   for(int j=1; j<=N; j++)
   {
   if(i==1 || i==N )
   {
   //Print "=" for first and last row
       System.out.print("=");
   }else if(j==1 || j==N){
       //Print "*" for first and last column
       System.out.print("*");
   }else
   {
           System.out.print(" ");
   }
   }
     
   //Move to the next line/row
   System.out.print(" ");
   }

return 0;
}

=============================***********************************===============================

Code for Patttern 2

Logic:
Combine code for simple right triangle star pattern(with n rows) and inverted right triangle star pattern(with n-1 rows).

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**

* C program to print half diamond star pattern series.

*/

#include<stdio.h>

int main()

{

int i, j, n, columns;

// Read number of columns from user

printf("Enter value of n:");

scanf("%d",&n);

columns=1;

for(i=1;i<=n*2;i++)

{

for(j=1; j<=columns; j++)

{

printf("*");

}

if(i < n)

{

/*

* Increment number of columns per row for upper part

*/

columns++;

}

else

{

/*

* Decrement number of columns per row for lower part

*/

columns--;

}

printf(" ");

}

return 0;

}

=============================***********************************===============================

Code for Patttern 3

Logic:
As like other star pattern problems here also spaces are arranged in a special way i.e. each row contains n - row_number spaces (where n is the total number of rows).
When you look to the total number of stars(*) in the equilateral triangle pattern you will notice that each row contains 2*rownumber - 1 stars.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**

* C program to print equilateral triangle or pyramid star pattern

*/

#include <stdio.h>

int main()

{

int i, j, n;

//Reads number of rows to be printed

printf("Enter value of n : ");

scanf("%d", &n);

for(i=1; i<=n; i++)

{

//Prints trailing spaces

for(j=i; j<n; j++)

{

printf(" ");

}

//Prints the pyramid pattern

for(j=1; j<=(2*i-1); j++)

{

printf("*");

}

printf(" ");

}

return 0;

}

=============================***********************************===============================