In this tutorial, we will discuss How to create hollow triangle pattern in C programming language
We can print many types of hollow triangle pattern in C programming language
We describe four hollow patterns in this post
Pattern 1
#include <stdio.h> #include <stdlib.h> int main() { int i,j,rows;//variable declaration //Input row value from user printf("Enter of rows you want\n"); scanf("%d",&rows); for(i=1; i<=rows; i++){//parent for loop for(j=i; j<=rows; j++){//print columns if(i==1 || j==i || j==rows){ printf("*"); } else{ printf(" "); } } printf("\n");//move to next line } getch(); return 0; }
When the above code is executed, it produces the following results:
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int i,j,rows;//variable declaration //Input row value from user printf("Enter of rows you want\n"); scanf("%d",&rows); for(i=1; i<=rows; i++){//parent for loop, for row of hollow triangle for(j=1; j<=i; j++){//print columns if(j==1 || j==i || i==rows){ printf("*"); } else{ printf(" "); } } printf("\n");//move to next line } getch(); return 0; }
When the above code is executed, it produces the following results:
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int i,j,rows;//variable declaration printf("Enter the number of rows you want: "); scanf("%d",&rows);//get input from user for rows for(i=1; i<=rows; i++){ for(j=1; j<i; j++){ printf(" "); } for(j=i; j<=rows; j++){ if(j==i || j==rows || i==1){ printf("*"); } else{ printf(" "); } } printf("\n");//move to next line for iterate } getch(); return 0; }
Program 4
#include <stdio.h> #include <stdlib.h> int main() { int i,j,rows; printf("Enter the number of rows you want: "); scanf("%d",&rows);//get input from user for rows for(i=1; i<=rows; i++){ for(j=i; j<rows; j++){ printf(" ");//print space for triangle } for(j=1; j<=i; j++){ if(i==rows || j==1 || j==i){ printf("*"); } else{ printf(" "); } } printf("\n");//move to next line for iterate } getch(); return 0; }
When the above code is executed, it produces the following results:
Suggested for you
Do-while loop in Java language
Similar post
Java program to print star pyramid pattern
C program to print star pyramid pattern
C++ program to print star pyramid pattern
Python program to print star pyramid pattern
Floyd’s triangle number pattern using for loop in C
Floyd’s triangle pattern using nested for loop in Java
Floyd’s triangle pattern using nested while loop in Java
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…