Print Right Diamond Number Pattern Type7
PRINTING PATTERN:
2
33
444
5555
5555
444
33
2
PREREQUISITE:
Basic knowledge of C language and use of loops.
ALGORITHM:
- Take the number of rows as input from the user and store it in any variable.(‘r‘ in this case).
- Run a loop ‘r’ number of times to iterate through each of the rows. From i=0 to i<r. The loop should be structured as for( i=0 ; i
- Use an if condition to to print the top half of the pyramid. if (i<=r/2) run a loop from j=0 to j<=i. The loop should be structured as for(j=0 ; j<=i ; j++)
- Inside this loop print count.
- Outside this loop increment count and print a newline
- Else decrement count
- Run a different loop from j=0 to j<r-i+1. The loop should be structured as for( j=0 ; j<r-i+1 ; j++).
- Inside this loop print count.
- After the loop print a newline.
CODE IN C:
#include int main() { int i,j,r,count; //declaring integer variables i,j for loops , r for number of rows printf("Enter the number of rows/columns :\n"); //asking user for the number of rows; scanf("%d",&r); //taking number of rows and saving in variable r count=2; //intialising count =3 for(i=1;i<=r;i++) //loop for number of rows { if(i<=r/2) { for(j=1;j<=i;j++) //loop to print digit in every column of a row { printf("%d",count); //printing digit } count++; //incrementing count printf("\n"); //printing newline } else { count--; for(j=0;j<r-i+1;j++) { printf("%d",count); } printf("\n"); } } }
TAKING INPUT:
DISPLAYING OUTPUT:
If you want to do the same code, but by using 2 inputs one for initializing the first term and then the number of times you want it to be printed.
Input:
3 4
Output:
3
44
555
6666
6666
555
44
3
Program:
#include<stdio.h>
int main()
{
int i,j,s,N,count=0;
scanf(“%d%d”,&s,&N);
for(i=s;count<4;count++)
{
for(j=0;j<count+1;j++)
printf(“%d”,i);
printf(“\n”);
i=i+1;
}
for(i=s+N-2;count>0;count–)
{
for(j=0;j<count-1;j++)
printf(“%d”,i);
printf(“\n”);
i=i-1;
}
return 0;
}