ad

Monday, April 14, 2025

C Program to Display a Hollow Diamond Shape with Star

C Program

C Program to print Hollow Diamond Star Pattern

Example :-


#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,rows;
printf("Enter the number \n");
scanf("%d",&rows);


for(i=1; i<=rows; i++){
for(j=rows; j>i; j--){
printf(" ");
}


printf("*");
for(j=1; j<(i-1)*2; j++){
printf(" ");
}


if(i==1){
printf("\n");
}


else{
printf("*\n");
}
}

for(i=rows-1; i>=1; i--){
for(j=rows; j>i; j--){
printf(" ");
}


printf("*");
for(j=1; j<(i-1)*2; j++){
printf(" ");
}


if(i==1){
printf("\n");
}


else{
printf("*\n");
}
}
return 0;
}


C Program to Display a Hollow Diamond Shape with Star

Explain Program

Step :1

Header and main function

#include <stdio.h>

#include <stdlib.h>

 int main()

{

    int i, j, rows;

  • #include <stdio.h>:  - use printf() , scanf() for input/output.
  • #include <stdlib.h>:  -use 
  • int i, j, rows; :-- Loop variables i & j, and rows is the number of rows input by the user

Step :2

User Input

printf("Enter the number \n");

scanf("%d", &rows);

  •  enter how many rows the upper half of the diamond 

  •  rows = 5, diamond will have 2 * rows - 1 = 9 lines total.

Step :3

Top Half of the Hollow Diamond

for(i = 1; i <= rows; i++) {

    for (j = rows; j > i; j--) {

        printf(" ");

    }

     printf("*");

     for(j = 1; j < (i - 1) * 2; j++) {

        printf (" ");

    }

     if (i == 1) {

        printf("\n");

    } else {

        printf ("*\n");

    }

}


How it work:

  • i represent to the current row number 
  • First inner loop (for (j = rows; j > i; j--))
  • Print spaces to align the star to the center.
  • printf("*"); Print the first star the line.
  • Second inner loops (for (j = 1; j < (i - 1) *2;  j++ ))
  • Prints the hollow spaces between two stars.
  • On line 1: no space.
  • On line 2: 1 space.
  • On line 3: 3 spaces.
  • Final if:
  •  first line (i == 1), only one * is printed (no second star).
  • From line 2 onwards, two *s are printed with space in between.

Step :4

Bottom Half of the Hollow Diamond

for(i = rows - 1; i >= 1; i--) {

    for(j = rows; j > i; j--) {

        printf(" ");

    }

     printf("*");

     for(j = 1; j < (i - 1) * 2; j++) {

        printf(" ");

    }

     if(i == 1) {

        printf("\n");

    } else {

        printf("*\n");

    }

}

How it work:

  • This is a mirror of the top half but in reverse.
  • i from rows - 1 down to 1 to avoid repeating the middle line.
  • Same logic applies:
  • Print leading spaces.
  • Print one *.
  • Print hollow middle spaces if needed.
  • Print second * if not the last line.

Step :5

 Program Exit

return 0;

}

successful program execution.

Step :6

Step :7

Step :8

Step :9

C Program to print Hollow Diamond Star Pattern

Output:-

C Program to print Hollow Diamond Star Pattern


Related Post :- 

c program to print full pyramid of star

C program to print inverted half


Sunday, April 13, 2025

C Program to Print Hollow Full Pyramid Using Stars Pattern

 

C Program to print Hollow Full pyramid star Pattern


C Program to print Hollow Full pyramid star Pattern

Example :-


#include<stdio.h>
int main()

{
int i, space, row, star=0;


printf("Enter The Number ");
scanf("%d",&row);


for(i = 0; i < row-1; i++) {
for(space = 1; space < row-i; space++) {
printf(" ");
}


for (star = 0; star <= 2*i; star++) {
if(star==0 || star==2*i)


printf("*");
else
printf(" ");
}
printf("\n");
}


for(i=0; i<2*row-1; i++){
printf("*");


}
return 0;
}


C Program to Print Hollow Full Pyramid Using Stars Pattern

Explain Program

Step :1

#include <stdio.h>

int main()

{

    int i, space, row, star = 0;


#include <stdio.h> :-  printf and scanf.

 int i, space, row, star;: Declares loop variables and input variable.

 row = number of levels of the pyramid.

 i = controls outer loop (rows).

 space = manages indentation before the stars.

 star = controls how many positions are printed per row.

Step :2

User Input

printf("Enter The Number ");

scanf("%d", &row);

User input how many rows they want for the pyramid.

 If input is 5, the pyramid 

Step :3

Hollow Pyramid

for(i = 0; i < row - 1; i++) {

    for(space = 1; space < row - i; space++) {

        printf(" ");

    }

     for(star = 0; star <= 2 * i; star++) {

        if(star == 0 || star == 2 * i)

            printf("*");

        else

            printf(" ");

    }

     printf("\n");

}

How it work:

  •  Loop 1)  for (i = 0; i < row - 1; i++)
  • Controls each row of the hollow pyramid .
  •  Runs row - 1 times (e.g., if row = 5, this loop runs 4 times).
  •  Loop 2)  for (space = 1; space < row - i; space++)
  • Adds spaces to align the stars in a pyramid shape.

Step :4

for(star = 0; star <= 2*i; star++)

Handle the character in the current row.

Always print * at the start (0) and end (2*i).

 In between, print a space to make it hollow.

 Step :5

Bottom Part:

for(i = 0; i < 2 * row - 1; i++) {

    printf("*");

}

loop prints a full base of stars at the bottom.


C Program to print Hollow Full pyramid star Pattern

Output :-

C Program to print Hollow Full pyramid star Pattern


Related Post :-


c program to print floyds triangle

c program to print pascals triangle


C Program to Print Hollow Square Star Pattern


C Program 


Q. C Program to Print Hollow Square  Star Pattern 

Example



#include <stdio.h>

int main()
{
   int i, j, N;

 
   printf("Enter number of rows: ");
   scanf("%d", &N);

   for(i=1; i<=N; i++)
   {
   
       for(j=1; j<=N; j++)
       {
           if(i==1 || i==N || j==1 || j==N)
           {
             
               printf("*");
           }
           else
           {
               printf(" ");
           }
       }

     
       printf("\n");
   }

   return 0;
}

C Program to Print Hollow Square  Star Pattern

Explain Program

Step :1

#include <stdio.h>

Standards Input Output library,  printf and scanf.

 Step :2

int main()

 entry point of the program.

i for iterating through rows,

 j for iterating through columns,

 N for the size of the square (number of rows and columns).

 Step :3

printf("Enter number of rows: ");

scanf("%d", &N);

 user to input a number (N), which defines the dimension of the square.

square will have N rows and N columns.

 Step :4

Outer Loop: 
Rows
for(i=1; i<=N; i++)
loop run from 1 to N (inclusive) to handlerow.

Step :5

Inner Loop:

 Columns

for(j=1; j<=N; j++)

 loop runs for each column within a row (again from 1 to N).

Step :6

Logic to Print the Border

if(i==1 || i==N || j==1 || j==N)

condition check current cell border of the square:

 First row: i == 1

 Last row: i == N

First column: j == 1

 Last column: j == N

 condition is true:

Step :7

printf("*");

Print an asterisk (*).

Step :8

printf(" ");

Print a space ( ) to create the hollow center.

Step :9

printf("\n");

After finishing row, move to the next line.

Step :10

 return 0;

 program finished successfully

Q. C Program to Print Hollow Square  Star Pattern

OutPut :-

C Program to Print Hollow Square  Star Pattern

Related Post :-

c program to print hourglass pattern

c program to print diamond pattern

c program to print rhombus star pattern


Friday, April 11, 2025

C Program to print Hourglass Star Pattern

  C Program

C Program to print Hourglass Pattern

Example


#include <stdio.h>
 
int main()
{
  int i, j, k, rows;
 
  printf("Enter the no. of rows: ");
  scanf("%d", &rows);
 
  printf("Output: \n\n");
 
  for (i = 1; i <= rows; i++)
  {
    for (k = 1; k < i; k++)
      printf(" ");
 
    for (j = i; j <= rows; j++)
      printf("* ");
 
    printf("\n");
  }
 
  for (i = rows - 1; i >= 1; i--)
  {
    for (k = 1; k < i; k++)
      printf(" ");
 
    for (j = i; j <= rows; j++)
      printf("* ");
 
    printf("\n");
  }
 
  return 0;
}


C Program to Print Hollow Square  Star Pattern

Explain Program

Step :1

#include <stdio.h>

 standard input/output library to use functions like printf() and scanf().

Step :2

int main()

{

  int n, a, k;

Declares three integer variables:

n: number of rows for the top half of the diamond.

 k: used for looping through rows.

 a: used for spaces and asterisks within each row.

Step :3

  printf("Enter number of rows\n");

  scanf("%d", &n);

input the number of rows determines the size of the diamond

Step :4

  for (k = 1; k <= n; k++)

  {

    for (a = 1; a <= n-k; a++)

      printf(" ");

 

    for (a = 1; a <= 2*k-1; a++)

      printf("*");

 

    printf("\n");

  }

 loop prints the top half of the diamond.

Spaces: n - k spaces before stars

 Stars: 2 * k - 1 stars

 k from 1 to n row has 2 * n - 1 stars. 

Step :5

Bottom Half of the Diamond 

  for (k = 1; k <= n - 1; k++)

  {

    for (a = 1; a <= k; a++)

      printf(" ");

 

    for (a = 1 ; a <= 2*(n-k)-1; a++)

      printf("*");

 

    printf("\n");

  }

This loop print the bottom half of the diamond.

  Spaces: k spaces.

 Star 2 * (n - k) - 1 stars

 loop  from k = 1 to n - 1 because the middle line 

Step :6

  return 0;

}

Ends the program.

Step :7

C Program to print Hourglass Pattern

Output 

C Program to print Hourglass Pattern


Related Post :-

C program to print pascals triangle

C program to print inverted half

Print Inverted Half Pyramid of Stars Pattern

C Program to Display a Diamond Pattern Using Asterisks

C Program  

C Program to Display a Diamond Pattern Using Asterisks 

Example:



#include <stdio.h>
int main()
{
  int n,a, k;

  printf("Enter number of rows\n");
  scanf("%d", &n);

  for (k = 1; k <= n; k++)
  {
    for (a = 1; a <= n-k; a++)
      printf(" ");

    for (a = 1; a <= 2*k-1; a++)
      printf("*");

    printf("\n");
  }

  for (k = 1; k <= n - 1; k++)
  {
    for (a = 1; a <= k; a++)
      printf(" ");

    for (a = 1 ; a <= 2*(n-k)-1; a++)
      printf("*");

    printf("\n");
  }

  return 0;
}

C Program to Display a Diamond Pattern Using Asterisks

Explain Program

Step :1

#include <stdio.h>

 input-output use printf() & scanf() function.

Step :2

int main()

The entry point of the C program. All execution .

Step :3

int n, a, k;

Three integer variables are declared:

 n: Number of rows for the top half of the diamond.

 k: Loop counter for rows.

 a: Loop counter for columns 

Step :4

printf("Enter number of rows\n");

scanf("%d", &n);

user to enter an integer (n).

 numbers determines the height of the top half  diamond.

Step :5

for (k = 1; k <= n; k++)

{

  for (a = 1; a <= n-k; a++)

    printf(" "); 

  for (a = 1; a <= 2*k-1; a++)

    printf("*"); 

  printf("\n");

}

This loop print the upper half of the diamond

 Outer loop (k = 1 to n)

Each iteration of k represents one row.

  First inner loop (a = 1 to n - k):Prints leading spaces to center-align the stars.

 Example: If k = 1, it prints n - 1 spaces.

 Spaces decrease as you move down (k increases).

 Second inner loop (a = 1 to 2*k - 1):

Prints the stars on each line.

 Stars increase by 2 in each row: 1, 3, 5, 7, ...

 Step :6

printf("\n");

After each row

Step :7

for (k = 1; k <= n - 1; k++)

{

  for (a = 1; a <= k; a++)

    printf(" ");

   for (a = 1 ; a <= 2*(n-k)-1; a++)

    printf("*");

   printf("\n");

}

Outer loops

   (k = 1 to n - 1)

 1 to n - 1 to the middles row  already printed to the top part.

 First inner loop (a = 1 to k):

Prints leading spaces, which increase with each row.

 Second inner loop (a = 1 to 2*(n-k) - 1):

Prints the stars.

 Number of stars decreases by 2 in each row: 2*(n-k)-1

 

C Program to Display a Diamond Pattern Using Asterisks

Output:


C Program to print Diamond Pattern

Related Post :-

c program to print Rhombus star pattern

c program to print floyds triangle

c program to print pascals triangle