C pattern

C Program to print solid square star pattern

C Program to print solid square star pattern

In this article, we will discuss the Program to Print solid square star pattern in C programming language

Solid rectangle pattern

In this post, we are going to learn How to write a program to print  solid square star pattern in C language using for loop, while loop and Do-while loop 

Program 1

C Program to print solid square star Using for loop

This program allows the user  to enter the size and then it will display solid square star pattern using for loop in  C programming language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,size;
    printf("Please enter the size\n");
    scanf("%d",&size); //get input from the user for size
    for (i=1; i<=size; i++){
  for (j=1; j<=size; j++){
  printf("*");
  }
  printf("\n");
}
getch();
    return 0;
}

 

When the above code is executed it produces the following output

Solid rectangle star pattern

Approach

  • This program requests the input size
  • The input(size) is stored in the variable “size
  • To iterate through the row, run through outer for loop from 1 to given size according to the loop structure for(i=1; i<=size; i++)
  • To iterate through the row, run through outer for loop from 1 to given size according to the loop structure for(j=1; j<=size; j++) ;
  • inside inner loop print star “*”;
  • This activity continues until the condition of outer while loop becomes false.

Program 2

C Program to print solid square star Using while loop

This program allows the user  to enter the size and then it will display solid square pattern using while loop in  C programming language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,size;
    printf("Please enter the size\n");
    scanf("%d",&size); //get input from the user for size
    i=1;
    while(i<=size){
            j=1;
  while(j<=size){
  printf("*");
   j++;
  }
  printf("\n");
  i++;
}
getch();
    return 0;
}

 

When the above code is executed it produces the following output

Solid rectangle star pattern

Approach

  • The program requests to input for the “size of the pattern”
  • The input stores in the variable “size”
  • To iterate through the row, run the outer while loop from 1 to given size according to the loop structure while(i<=size)
  • To iterate through the column, run the inner while loop from 1 to given size according to the loop structure while(j<=size);
  • inside inner loop print “*”
  • This activity continues until the condition of outer while loop becomes false

Program 3

C Program to print solid square star Using the do-while loop

This program allows the user  to enter the size and then it will display solid square pattern using the do-while loop in  C programming language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,size;
    printf("Please enter the size\n");
    scanf("%d",&size); //get input from the user for size
    i=1;
    do{
            j=1;
do{
  printf("*");
   j++;
  }while(j<=size);
  printf("\n");
  i++;
}while(i<=size);
getch();
    return 0;
}

 

When the above code is executed it produces the following output

Solid rectangle star pattern

Approach

  • The program requests to input for the “size of the pattern”
  • The input stores in the variable “size”
  • To iterate through the row, run the outer do-while loop from 1 to given size according to the loop structure while(i<=size);
  • To iterate through the column, run the inner do-while loop from 1 to given size according to the loop structure while(j<=size);
  • inside inner loop print star
  • This activity continues until the condition of outer while loop becomes false

Suggested for you

for loop in C language

while  loop in C language

do-while  loop in C language

Nested for loop in C language

Nested while loop in C language

 

 

Similar post

C Program to print solid square star pattern

Java Program to print solid square star pattern

C++ Program to print solid square star pattern

 Print hollow square star pattern in Java

Print hollow square star pattern in C

Print hollow square star pattern in C++

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.