Table of Contents
C program to Diamond pattern program
In this article, we will discuss the programming concept of C program to Diamond star pattern program
we can create star number, alphabet, binary patterns using loops (for, while and do-while loop) in C language
In this post, we are going to learn how to create a diamond pattern in C language using the given symbol
C code to the Diamond pattern using for loop
This program allows the user to enter the number of rows and the symbol then the program displays the diamond pattern with the given symbol using for loop in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int rows,i,j,Num_Of_space=1; char ch; printf("Enter number of rows\n"); scanf("%d%c",&rows,&ch); printf("Enter the symbol as you wish:"); scanf("%c",&ch); Num_Of_space=rows-1; for(i=1; i<=rows; i++){ for(j=1; j<=Num_Of_space; j++) printf(" "); Num_Of_space--; for(j=1; j<=2*i-1; j++) printf("%c",ch); printf("\n"); } Num_Of_space=1; for(i=1; i<=rows-1; i++){ for(j=1; j<=Num_Of_space; j++) printf(" "); Num_Of_space++; for(j=1; j<=2*(rows-i)-1; j++) printf("%c",ch); printf("\n"); } getch(); return 0; }
When the above code is executed, it produces the following result
C code to Diamond using while loop
This program allows the user to enter the number of rows and the symbol then the program displays the diamond pattern with the given symbol using while loop in C language
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int rows,i,j,Num_Of_space=1; char ch; printf("Enter number of rows\n"); scanf("%d%c",&rows,&ch); printf("Enter the symbol as you wish:"); scanf("%c",&ch); Num_Of_space=rows-1; i=1; while(i<=rows){ j=1; while(j<=Num_Of_space){ printf(" "); j++; } Num_Of_space--; j=1; while(j<=2*i-1){ printf("%c",ch); j++; } printf("\n"); i++; } Num_Of_space=1; i=1; while(i<=rows-1){ j=1; while(j<=Num_Of_space){ printf(" "); j++; } Num_Of_space++; j=1; while( j<=2*(rows-i)-1){ printf("%c",ch); j++; } printf("\n"); i++; } getch(); return 0; }
When the above code is executed, it produces the following result
C code to Diamond using the do-while loop
This program allows the user to enter the number of rows and the symbol then the program displays the diamond pattern with the given symbol using the do-while loop in C language
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int rows,i,j,Num_Of_space=1; char ch; printf("Enter number of rows\n"); scanf("%d%c",&rows,&ch); printf("Enter the symbol as you wish:"); scanf("%c",&ch); Num_Of_space=rows-1; i=1; do{ j=1; do{ printf(" "); j++; }while(j<=Num_Of_space); Num_Of_space--; j=1; do{ printf("%c",ch); j++; } while(j<=2*i-1); printf("\n"); i++; }while(i<rows); Num_Of_space=1; i=1; do{ j=1; do{ printf(" "); j++; }while(j<=Num_Of_space); Num_Of_space++; j=1; do{ printf("%c",ch); j++; }while( j<=2*(rows-i)-1); printf("\n"); i++; }while(i<rows); getch(); return 0; }
When the above code is executed, it produces the following result
Suggested post you
Nested while loop in C language
Nested do while loop in C language
Similar post
C++ pyramid star pattern – using loops
Java pyramid star pattern – using loops
C pyramid star pattern – using loops
C++ program to Inverted Pyramid star pattern
Java program to Inverted Pyramid star pattern
C++ code to Hollow Pyramid star pattern
Java code to Hollow Pyramid star pattern
Print Hollow Inverted Pyramid pattern in C++ language
Print Hollow Inverted Pyramid pattern in Java language
Java Hollow diamond star pattern
C++ Hollow diamond star pattern
Display diamond shape in Java language
Display diamond shape in C language
Display diamond shape in C++ language