Half Right Diamond Number Pattern:
Here the number of lines is taken as input. The aforesaid pattern is divided into two halves while implemented. In The first half, the number of elements are increased and in the second half, the number of elements decreased. For each half two for loops are used to print the elements. The number of elements are increased by 1 and in the second half, the number of elements are decreased by 1. The pattern is only possible if the number of lines is even.
INPUT: Number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: [taking the inputs]
Read n [number of lines]
Step 2: [printing the pattern]
If n modulus 2=0 then
For i=1 to n/2 repeat
For j=1 to i repeat
If j=i or i=1 then
Print i
Else if j≠i then
Print i
Print *
[End of ‘if’]
[End of ‘for’ loop]
Move to the next line
[End of ‘for’ loop]
For i=n/2 to 1 repeat
For j=1 to i repeat
If j=i or i=n then
Print i
Else if j≠i then
Print i
Print *
[End of ‘if’]
[End of ‘for’ loop]
Move to the next line
[End of ‘for’ loop]
else
print "Please give correct input(no. of lines should be even)
[End of ‘if’]
Step 3: Stop
for(i=1;i<=n/2;i++)------------------------------------------------------------------- n/2
{ for(j=1;j<=i;j++)----------------------------------- i
{ if(i==1||j==i)
printf("%d",i);
else if(j!=i)
{ printf("%d",i);
printf("*"); }
}
printf("\n");
}
//printing the second half of the pattern where the number of elements
//are decreasing
for(i=n/2;i>=1;i--)---------------------------------------------------- n/2
{ for(j=1;j<=i;j++)-------------------------------------------- i
{ if(j==i||i==n)
printf("%d",i);
else if(j!=i)
{ printf("%d",i);
printf("*");
}
}
printf("\n"); }
The complexity is: O(2*(n/2*i))=O()
Related
Contributed by