Hallow Diamond pattern in C
In this tutorial, we'll learn how to use the for loop in the programming language C to display the hollow diamond star pattern.
Here, we demonstrate how to display the hollow diamond star pattern program employing nested for loops as well as how to receive user input employing the C language's Scanf() function.
The user can enter any number they like, and a hollow diamond star pattern will be generated for them.
Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows: \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;
}
Output
![Hallow Diamond pattern in C](https://static.tutorialandexample.com/c/hallow-diamond-pattern-in-c1.png)
Example2
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
for(int j=i; j<=n; j++)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
if(k==1 || k==(2*i-1)) printf("*");
else printf(" ");
}
printf("\n");
}
for(int i=n-1; i>=1; i--)
{
for(int j=n; j>=i; j--)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
if(k==1 || k==2*i-1) printf("*");
else printf(" ");
}
printf("\n")
}
return 0;
}
Output
![Hallow Diamond pattern in C](https://static.tutorialandexample.com/c/hallow-diamond-pattern-in-c2.png)
Utilizing a while loop, display the hollow diamond pattern.
- With the help of the while loop and the C programming language, we will learn how to display the hollow diamond star pattern in this program.
- In this section, we demonstrate a hollow diamond star pattern program that uses nested while loops and the C language's Scanf() function to collect user input.
- The user can enter whatever number they like, and it will produce the hollow diamond star pattern.
Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b, c;
printf("Enter the number of rows\n");
scanf("%d",&c);
a=1;
while(a<=c){
b=c;
while( b>a){
printf(" ");
b--;
}
printf("*");
b=1;
while(b<(a-1)*2){
printf(" ");
b++;
}
if(a==1){
printf("\n");
}
else{
printf("*\n");
}
a++;
}
a=c-1;
while(a>=1){
b=c;
while(b>a){
printf(" ");
b--;
}
printf("*");
b=1;
while(b<(a-1)*2){
printf(" ");
b++;
}
if(a==1){
printf("\n");
}
else{
printf("*\n");
}
a--;
}
return 0;
}
Output
![Hallow Diamond pattern in C](https://static.tutorialandexample.com/c/hallow-diamond-pattern-in-c3.png)
Hallow Diamond square star pattern in C
An appealing and eye-catching pattern that combines geometric symmetry and creativity is the square star hollow pattern. This design creates a complex and captivating arrangement by carefully arranging star symbols inside a square frame. A layout that can be both visually appealing and fascinating results from the interaction between the structured square shape and the heavenly allure of stars.
Algorithm
- This pattern's algorithm has characteristics comparable to those of the square star pattern, with the exception that stars from the initial and final rows and columns must be printed here.
- We just print the space character at non-boundary positions.
- Using the scanf function, receive the user-provided input of the total number of stars on each side of the square. Let N be the value.
- For printing a square star pattern, we are going to utilize two for loops.
- The outer portion of the for loop will repeat N times. It will print one row of the pattern in a single iteration.
- We'll include an if check inside the inner for loop to determine the pattern's boundary positions and print the star (*) appropriately.
Example
#include<stdio.h>
int main()
{
int i, j, k, m, a = 1,b = 0;
for(i = 5;i > 0;i--)
{
for(j = i;j > 0;j--)
{
printf("*");
}
for(k = 1;k < a;k++)
{
printf(" ");
}
a = a + 2;
for(m = i;m > 0;m--)
{
printf("*");
}
printf("\n");
}
for(i = 1;i <= 4;i++)
{
for(j = 0;j <= i;j++)
{
printf("*");
}
for(k = 6;k > b;k--)
{
printf(" ");
}
b = b + 2;
for(m = 0;m <= i;m++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
![Hallow Diamond pattern in C](https://static.tutorialandexample.com/c/hallow-diamond-pattern-in-c4.png)
Explanation
However, in this instance, each character needs to be the same.
The initial and last rows in this pattern each have one star, while the rows that follow all have precisely two stars.
Rather than using stars, we are able to show any character; simply replace the * in the code alongside that character.