In this tutorial, we will discuss the concept of the C program to Display diamond number pattern using while loop
In this post, we will learn how to make different diamond number pattern using while loop in C language
We can use for loop, while loop or do while loop to display different diamond Number patterns.
Here, we will use while loop to print diamond Number patterns in C language.
The following C program requests the user to enter the number of rows as the user wanted and the program will display the number of rows in diamond number pattern according to the user input
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int rows; printf("Enter the number of rows: "); scanf("%d",&rows); int row=1,i,j,k; i=1; while(i<=rows){ //print upper part for(j=1; j<=rows-i; j++){ printf(" ");//print space } k=1; while(k<=i){ printf("%d",k);//print number to upper part printf(" "); k++; } printf("\n"); i++; } i=1; while(i<=rows-1){ //print lower part j=1; while(j<=i){ printf(" ");//print space j++; } k=1; while(k<=rows-i){ printf("%d",k);//print num ber to lower part printf(" "); k++; } printf("\n");//move to next line i++; } getch(); return 0; }
When the above code is executed, it produces the following result
Enter the number of rows: 6 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int i,j,k,l,rows; printf("|Enter the number of rows"); scanf("%d",&rows); i=1; while(i<=rows){ //print upper part j=1; while(j<=rows-i){ printf(" ");//print initial space for upper part j++; } k=i; while(k>=1){ printf("%d",k); k--; } l=2; while(l<=i){ printf("%d",l); l++; } printf("\n"); i++; } i=rows-1; while(i>=1){ //print lower part j=0; while(j<=rows-1-i){ printf(" ");//print initial space for lower part j++; } k=i; while(k>=1){ printf("%d",k); k--; } l=2; while(l<=i){ printf("%d",l); l++; } printf("\n"); i--; } return 0; }
When the above code is executed, it produces the following result
Enter the number of rows: 6 1 212 32123 4321234 543212345 65432123456 543212345 4321234 32123 212 1
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int rows,i,j; int row=1; printf("Enter the number of rows: "); scanf("%d",&rows); i=rows/2; while(i>0){ //print upper part j=1; while(j<=i){ printf(" "); j++; } j=1; while(j<=row){ printf("%d",row); printf(" "); j++; } printf("\n"); row++; i--; } i=0; while(i<=rows/2){ j=1; while(j<=i){ printf(" "); j++; } j=row; while(j>=1){ //print lower part printf("%d",row); printf(" "); j--; } printf("\n"); row--; i++; } getch(); return 0; }
When the above code is executed, it produces the following result
Enter the number of rows: 8 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
Similar post
Java program to Display diamond number pattern using while loop
C++ code to Display diamond number pattern using while loop
Suggested for you
Nested while loop in C language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…