0% found this document useful (0 votes)
91 views7 pages

Explanation of Sequential Diamond Pattern

The document provides an overview and explanation of the steps to write a C program to print a numeric pattern based on an input number. It explains that the pattern can be split into two halves - the top half from row 1 to the middle row, and the bottom half forming an upside down pyramid. It then outlines the key parts of the code: using loops to print the spaces and numbers, with conditions to handle the changing patterns in each half. The full code combines these elements, using nested for loops and conditions to systematically print the desired output for any given input number.

Uploaded by

Vbgsd Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views7 pages

Explanation of Sequential Diamond Pattern

The document provides an overview and explanation of the steps to write a C program to print a numeric pattern based on an input number. It explains that the pattern can be split into two halves - the top half from row 1 to the middle row, and the bottom half forming an upside down pyramid. It then outlines the key parts of the code: using loops to print the spaces and numbers, with conditions to handle the changing patterns in each half. The full code combines these elements, using nested for loops and conditions to systematically print the desired output for any given input number.

Uploaded by

Vbgsd Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Question: Write a C program to print the following pattern: For example, if

n = 4 then the pattern is:

OVERVIEW

1. The way this pattern will be solved is by splitting the code into 2 halves:

Because if you notice, from the 1st row to the middle row, the pattern
follows a sequence (A normal pyramid structure)
But after that it breaks to follow another sequence (An upside down
pyramid)
 So the first part of the code will be related to the pattern from row 1 to the
middle row and the 2nd part will be related to the upside down pyramid (rest
of the rows).
 Also note that the middle row has the same row no. as the input value
ie. If input is 4, middle row is row 4
if input is 5, middle row is row 5… so we can store the input with 2 variables
1 to store the input other to point to the middle row number.
DIFFERENT PARTS OF THE CODE

1. So the way to obtain this pattern is by making the first half of the
pattern (from row 1 to middle row) with 1 set of code and the other half
with another set of code.

 The first step is to use a loop 1 to move through the first row to the middle row
 Then we use a 2nd for-loop for printing the spaces and another 3rd loop to print the
numbers in each row (Both the 2nd and 1st loops are inside loop 1)
 In this example middle row is row 4 therefore the for-loop starts from 1 and ends
at 4

for (i=1; i<=middle; i++)  Loop 1 which will go from the 1st row to the middle row
PRINTING THE SPACES

2. We also notice that the no. of spaces before the first number in each
row decreases by 2 from the top row to the middle row.
 To print these spaces, we can use a 2nd for-loop again (inside the 1st for-
loop)
Starting from 1 and end condition being the middle row,

for (space=1; space<=middle-i; i++)

 Under this loop, we write a print statement for 2 spaces


So it looks like this

for (space=1; space<=middle-i; i++)


printf (“ “)
 here in the first iteration of the loop (middle-i) middle = 4 and i is 1
So the loop goes from 1 to 3 [ as middle-i = 4–1 ] so
,printing the first 6 spaces of the first row as the printf statement prints 6
blank spaces every time.
 In the second iteration of the loop, middle = 4 and I is 2
So the loop goes from 1 to 2 [ as middle-i = 4–2 ]
, printing 2 spaces in the second row

So on until the middle row


PRINTING THE NUMBERS

Now that the spaces have been taken care of, we need to print the numbers.

1. We can see that for the input ‘4’ – there are 4 numbers in the middle
row.
2. We also notice that the numbers that are printed in each row increases
by 1.
 The number of values printed in row 1 is 1
 The number of values printed in row 2 is 2
 The number of values printed in row 3 is 3
 Until the middle row, this pattern holds – therefore we can use a while-loop to print
the numbers in this order until the middle row.

k=1;

while (k<=i) - the condition is that k is less than or equal to the


row value

printf ("%d ",j); -the value of j is printed from 1

j++;

k++;

Printf (“\n”);
COMPILATION

Compiling all the previous loops into one, the 1st part of the program looks like :

This is the first part of the code, which will only print the 1st row to the middle row:

(for sample input value 4)


SECOND PART

- The second part of the code is very similar to the first part, in that the loops are the
same, but the only differences are the conditions.
- There are 3 loops again. The 2nd and 3rd being inside the first.
- Since we need to know the number of the remaining rows for the first loop we will
use
rows=(2*n)-1;
botrows=(rows/2); (The bottom remaining rows which we have to
print are half of the total rows)
Then
for (l=1; l<=botrows; l++) -1st loop for going through all the remaining rows
{
for (space=1; space<=l; space++)
{ printf (" ");} -2nd loop for spaces

for(k=1; k<=botrows+1-l; k++)


{printf ("%d ",j); -3rd loop for printing the letters
j++;
}

printf("\n"); -new line code to go to next line after printing 1 row


}
FINAL CODE

- In the first loop certain conditions are added as the question had further requirements
(Hint: 1. You may use if condition to check if an integer is less than 10 then within
the if condition use a space after that integer inside the printf statement else print
the number as it is.)

k=1;

while (k<=i)
{if (j<10)
printf ("%d ",j);
else
printf ("%d ",j);
j++;
k++;
}

You might also like