- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C Remove Extra Spaces
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
- C Programming Test
- C Programming Test
C Program to Print Diamond Pattern
In this article, you will learn and get code about printing of diamond pattern in following ways:
- Diamond Pattern of Stars
- Diamond Pattern of Numbers
- Diamond Pattern of Alphabets
Print Diamond Pattern of Stars
Let's create a program that asks from user to enter the row-size of upper-half diamond to print the diamond pattern of stars. For example, if user enters 5 as row size, then a diamond of stars gets printed of size 5*2-1 or 9 rows.
#include<stdio.h> #include<conio.h> int main() { int i, j, row, space; printf("Enter Number of Rows: "); scanf("%d", &row); space = row-1; for(i=1; i<=row; i++) { for(j=1; j<=space; j++) printf(" "); space--; for(j=1; j<=(2*i-1); j++) printf("*"); printf("\n"); } space = 1; for(i=1; i<=(row-1); i++) { for(j=1; j<=space; j++) printf(" "); space++; for(j=1; j<=(2*(row-i)-1); j++) printf("*"); printf("\n"); } getch(); return 0; }
This program was build and run under Code::Blocks IDE. Here is its output:
Now enter the number of rows say 10 to print diamond pattern that expands upto row-1 line. That is, with row number 10, it will print diamond pattern with 9 lines as shown in the output given below:
Program Explained
If user enters 10 as size of diamond, then always remember these things to print upper half of diamond:
- At first row, print 9 spaces, and one star
- At second row, print 8 spaces, and three star
- At third row, print 7 spaces, and five star
- At fourth row, print 6 spaces, and seven star
- At fifth row, print 5 spaces, and nine star
- At sixth row, print 4 spaces, and eleven star
- At seventh row, print 3 spaces, and thirteen star
- At eighth row, print 2 spaces, and fifteen star
- At ninth row, print 1 space, and seventeen star
- At tenth row, print ninteen star
And for lower half of diamond:
- At first row, print 1 space, and seventeen star
- At second row, print 2 spaces, and fifteen star
- At third row, print 3 spaces, and thirteen star
- At fourth row, print 4 spaces, and eleven star
- At fifth row, print 5 spaces, and nine star
- At sixth row, print 6 spaces, and seven star
- At seventh row, print 7 spaces, and five star
- At eighth row, print 8 spaces, and three star
- At ninth row, print 9 spaces, and one star
The dry run of above program goes like, supposing user input as 10:
- 10 gets initialized to the variable row
- Using the statement
space = row-1;
9 gets initialized to space - Now program flow executes the for loop
- At first run of for loop with i=1, the dry run goes like:
- Inside the loop, 1 gets initialized to i and checks whether it is less than or equal to row or not
- The condition evaluates to be true, because obviously the value of i (1) is less than the value of row (10)
- Therefore program flow goes inside the loop's body and executes another for loop
- This time, 1 gets initialized to j and checks whether it is less than or equal to the value of space (9) or not
- The condition evaluates to be true
- Therefore program flow goes inside the loop body and executes only one statement, that is,
printf(" ");
that prints a space - Now the variable j gets incremented and again program flow goes to the loop's condition part and checks whether the updated value or incremented value of j (2) is less than or equal to the value of space (9) or not
- The condition again evaluates to be true. Therefore program flow again goes inside the loop, and prints another space
- In this way printing of space continues until the value of j becomes greater than the value of space
- Therefore, total of 9 space gets printed at first row
- And then decrement the value of space to print one less space on next row (than previous one)
- Now the program flow goes to the second for loop part (present inside the first outer for loop)
- There, 1 gets initialized to j and checks whether it is less than or equal to the value of ((2*i)-1) or not
- Because the value of i was 1 at first run, therefore compares j with (2*i)-1 or (2*1)-1 or 1
- The condition evaluates to be true
- Therefore program flow goes inside the loop and prints a star (*)
- Then increments the value of j and compares again with ((2*i)-1)
- This time, the value of j (2) is not less than or equal to (2*i)-1, that is 1
- Therefore condition evaluates to be false, and the program flow exits the loop
- And using newline character (\n), program flow now starts the next output (printing) thing from new line
- At second run of for loop with i=2, dry run goes like:
- Using first for loop, prints 8 spaces. Process goes same from step no.1 to step no.10 as mentioned above with updated value of i
- Again decrements the value of space to print one less space next time. Now space=8
- Using second for loop, prints 3 stars. Process goes same from step no.12 to step no.19 as mentioned above with update value of i
- Then process step no.20
- At third run of for loop with i=3, dry run goes like:
- Process the similar operation from step no.1 to step no.10 with new value of i (3). That is, prints 7 spaces
- Decrements the value of space. Now space=7
- Process the similar operation given from step no.12 to step no.19 with new value of i (3). That is, print 5 stars
- Process step no.20
- Continue executing the loop, until the value of i becomes 11
- Because 11 is not less than or equal to row (10), therefore condition evaluates to be false, and the program flow exits from this loop
- Process in similar way to print lower half of diamond
- With initial value of space as 1
- Because, at first row of lower half, we only have to print single space
Print Diamond Pattern of Numbers
Here is another program of printing diamond pattern of numbers. In each row, the number starts with 1. The program is similar as of previous one. Except, in place of star, print the number using a variable say num (initialized with 1 at start of the program). Increment the value of num after each print.
Note - Always remember to initialize num with 1 after the statement printf("\n");
to start with 1 for each row
#include<stdio.h> #include<conio.h> int main() { int i, j, row, space, num=1; printf("Enter Number of Rows: "); scanf("%d", &row); space = row-1; for(i=1; i<=row; i++) { for(j=1; j<=(space); j++) printf(" "); space--; for(j=1; j<=(2*i-1); j++) { printf("%d", num); num++; } printf("\n"); num=1; } space = 1; for(i=1; i<=(row-1); i++) { for(j=1; j<=(space); j++) printf(" "); space++; for(j=1; j<=(2*(row-i)-1); j++) { printf("%d", num); num++; } printf("\n"); num=1; } getch(); return 0; }
Here is the output produced by above program assuming that user has entered 5 as the size of diamond:
Print Diamond Pattern of Alphabets
This program is almost same as of previous one. In place of number, use a character using a variable say ch of char type. Initialize it with A. Rest of the things are similar with previous program.
#include<stdio.h> #include<conio.h> int main() { int i, j, row, space; char ch='A'; printf("Enter Number of Rows: "); scanf("%d", &row); space = row-1; for(i=1; i<=row; i++) { for(j=1; j<=(space); j++) printf(" "); space--; for(j=1; j<=(2*i-1); j++) { printf("%c", ch); ch++; } ch='A'; printf("\n"); } space = 1; for(i=1; i<=(row-1); i++) { for(j=1; j<=(space); j++) printf(" "); space++; for(j=1; j<=(2*(row-i)-1); j++) { printf("%c", ch); ch++; } ch='A'; printf("\n"); } getch(); return 0; }
Here is its output assuming the user input 5:
Same Program in Other Languages
« Previous Program Next Program »
Follow/Like Us on Facebook
Subscribe Us on YouTube