
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program to print hollow pyramid and diamond pattern
Here we will see how to generate hollow pyramid and diamond patterns using C. We can generate solid Pyramid patterns very easily. To make it hollow, we have to add some few tricks.
Hollow Pyramid
For the pyramid at the first line it will print one star, and at the last line it will print n number of stars. For other lines it will print exactly two stars at the start and end of the line, and there will be some blank spaces between these two starts.
Example Code
#include <stdio.h> int main() { int n, i, j; printf("Enter number of lines: "); scanf("%d", &n); for(i = 1; i<=n; i++) { for(j = 1; j<=(n-i); j++){ //print the blank spaces before star printf(" "); } if(i == 1 || i == n){ //for the first and last line, print the stars continuously for(j = 1; j<=i; j++) { printf("* "); } } else { printf("*"); //in each line star at start and end position for(j = 1; j<=2*i-3; j++) { //print space to make hollow printf(" "); } printf("*"); } printf("
"); } }
Output
Enter number of lines: 20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Hollow Diamond
For the diamond at the first line and at the last line it will print one star. For other lines it will print exactly two stars at the start and end of the line, and there will be some blank spaces between these two starts. Diamond has two parts. The upper half and the lower half. At the upper half we have to increase the space count, and for the lower half, we have to decrease the space count. Here the line numbers can be divided into two parts by using another variable called mid.
Example Code
#include <stdio.h> int main() { int n, i, j, mid; printf("Enter number of lines: "); scanf("%d", &n); if(n %2 == 1) { //when n is odd, increase it by 1 to make it even n++; } mid = (n/2); for(i = 1; i<= mid; i++) { for(j = 1; j<=(mid-i); j++){ //print the blank spaces before star printf(" "); } if(i == 1) { printf("*"); } else { printf("*"); //in each line star at start and end position for(j = 1; j<=2*i-3; j++){ //print space to make hollow printf(" "); } printf("*"); } printf("
"); } for(i = mid+1; i<n; i++) { for(j = 1; j<=i-mid; j++) { //print the blank spaces before star printf(" "); } if(i == n-1) { printf("*"); } else { printf("*"); //in each line star at start and end position for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow printf(" "); } printf("*"); } printf("
"); }
Output
Enter number of lines: 15 * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- Related Articles
- Program to print hollow pyramid and diamond pattern in C++
- Program to print pyramid pattern in C
- Program to print Diamond Pattern in C
- C program to print hollow rectangle star pattern
- Program to print Inverse Diamond pattern on C++
- C++ Program to Print Hollow Right Triangle Star Pattern
- Java Program to Print Pyramid Star Pattern
- Swift program to Print Pyramid Star Pattern
- Swift Program to Print Numeric Pyramid Pattern
- Golang Program to Print Pyramid Star Pattern
- Haskell Program to Print Pyramid Star Pattern
- Java Program to Print Diamond Star Pattern
- Haskell Program to Print Diamond Star Pattern
- Swift program to Print Diamond Star Pattern
- Golang Program To Print Diamond Star Pattern
