Print Rectangle Star Pattern

PRINTING PATTERN:

******

******

******

******

PREREQUISITE:

Basic knowledge of C language and use of loops.

ALGORITHM:

  1. Take the number of rows and columns as input from the user ( length and breadth of the rectangle) and store it in two different variables. (‘r’ and ‘c’ in this case)
  2. Run a loop ‘r’ number of times to iterate through all the rows. From i=0 to i<r. The loop should be structured as for(i=0 ; i<r ; i++)
  3.  Run a nested loop ‘c’ times to iterate though each column of a row. From j=0 to j<c. The loop should be structured as for(j=0 ; j<c ; j++).
  4. Inside the nested loop print star to print a star in each column of  a row.
  5. In the main loop print a new line to move to the next line.

CODE IN C:

#include<stdio.h>
int main()
{
int i,j,r,c; //declaring integers i,j for loops and r,c for number of rows and columns
printf("Enter the number of rows\n"); //asking user for number of rows
scanf("%d",&r); //saving number of rows in a variable r
printf("Enter the number of columns\n"); //asking user for number of columns
scanf("%d",&c); //saving number of columns in a variable c
   for(int i=0;i<r;i++) //outer loop for number of rows
      {
         for(int j=0;j<c;j++) //inner loop for number of columns
            {
              printf("*"); //printing stars in each column of a row
            }
         printf("\n"); //printing new line after a row
      }
}

TAKING INPUT:
DISPLAYING OUTPUT:

One comment on “Print Rectangle Star Pattern”


  • Lalit Kr.

    Code in JAVA
    import java.util.*;
    public class Star4 {
    public static void main(String arg[])
    {
    Scanner s = new Scanner(System.in);
    System.out.println(“Enter no of rows: “);
    int m = s.nextInt();
    System.out.println(“Enter no of columns: “);
    int n = s.nextInt();

    for(int i=0; i<m; i++)
    {
    for(int j=0; j<n; j++)
    {
    System.out.print("*");
    }
    System.out.println();
    }
    }
    }