Print Number Star Right Diamond Pattern Type2

PRINTING PATTERN:

1

2*2

3*3*3

4*4*4*4

4*4*4*4

3*3*3

2*2

1

PREREQUISITE:

Basic knowledge of C language and use of loops.

ALGORITHM:

  1. Take the number of rows as input from the user and store it in any variable.(‘r‘ in this case).
  2. 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=1 ; i<=r : i++).
  3. 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=1; j<=i ; j++)
  4. Run an if statement if(j!=1). If true the print star and count else only print count.
  5. Then in the outer if statement increment count. Then print a newline
  6. Inside this loop print count.
  7. Outside this loop increment count and print a newline
  8. Else decrement count and run a loop from j=0 to j<r-i. The loop should be structured as for(j=0; j<\r-i ; j++). inside the loop run an if statement if(j!=0) then print star and count else just print count.
  9. After the loop print a newline.

CODE IN C:

#include<stdio.h>
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=1;                                               //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
          {
            if(j!=1)
              {
                printf("*%d",count);                    //printing digit
              }
            else
              {
                printf("%d",count);                     //printing digit
              }
          }         
        count++;                                       //incrementing count
        printf("\n");                                  //printing newline

      }
    else
      {
        count--;

        for(j=0;j<r-i+1;j++)
          {
            if(j!=0)
              {
                 printf("*%d",count);
              }
            else
              {
                 printf("%d",count);
              }
          }

         printf("\n");
       }
  }
}

TAKING INPUT:DISPLAYING OUTPUT:

4 comments on “Print Number Star Right Diamond Pattern Type2”


  • Sanjay

    #python solution
    n=int(input())
    for i in range(1,n+1):
    s=”
    for j in range(i):
    s+=”*”+str(i)
    print(s[1:])
    for i in range(n,0,-1):
    s=”
    for j in range(i):
    s+=”*”+str(i)
    print(s[1:])
    print()


  • Pratiksha

    #in python
    def pattern(n):
    for i in range(1,n+1):
    for j in range(1,i+1):
    if(j!=1):
    print(“*”+str(i),end=””)
    else:
    print(i,end=””)
    print()
    for i in range(n,0,-1):
    for j in range(1,i+1,1):
    if(j!=1):
    print(“*”+str(i),end=””)
    else:
    print(i,end=””)
    print()

    if __name__ == “__main__” :
    n=int(input(“Enter number of rows::”))
    pattern(n)


  • Pratiksha

    def pattern(n):
    for i in range(1,n+1):
    for j in range(1,i+1):
    if(j!=1):
    print(“*”+str(i),end=””)
    else:
    print(i,end=””)
    print()
    for i in range(n,0,-1):
    for j in range(1,i+1,1):
    if(j!=1):
    print(“*”+str(i),end=””)
    else:
    print(i,end=””)
    print()

    if __name__ == “__main__” :
    n=int(input(“Enter number of rows::”))
    pattern(n)