In this program, we will make a C program to print diamond patterns of numbers & a C program to print diamond patterns of stars using nested for loop and if-else statements.
C program to print diamond pattern of number
#include<stdio.h>
int main(){
int n;
printf("Enter Number:");
scanf("%d",&n);
int i=1;
while(i<=n)
{
for(int j=1;j<=n-i;j++)
printf(" ");
for(int j=1;j<=i;j++)
printf("%d", j);
for(int j=i-1;j>0;j--)
printf("%d", j);
printf("\n");
i++;
}
i=n-1;
while(i>0)
{
for(int j=1;j<=n-i;j++)
printf(" ");
for(int j=1;j<=i;j++)
printf("%d", j);
for(int j=i-1;j>0;j--)
printf("%d", j);
printf("\n");
i--;
}
}
#include <stdio.h> int main() { int i, j, p; for (i = 1; i <= 5; i++) { p = 1; for (j = 1; j <= 9; j++) { if ((j <= 5 - i) || (j >= 5 + i)) { printf(" "); } else { printf("%d", p); p++; } } printf("\n"); } for (i = 1; i <= 4; i++) { p = 1; for (j = 1; j <= 9; j++) { if ((j <= i) || (j >= 10 - i)) { printf(" "); } else { printf("%d", p); p++; } } printf("\n"); } return 0; }
Output:
C program to print diamond pattern of star
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 9; j++)
{
if ((j <= 5 - i) || (j >= 5 + i))
{
printf(" ");
}
else
{
printf("*");
}
}
printf("\n");
}
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 9; j++)
{
if ((j <= i) || (j >= 10 - i))
{
printf(" ");
}
else
{
printf("*");
}
}
printf("\n");
}
return 0;
}
About the author:-
Vipul Khokhar makes this program.
Thanks for reading the C program to print diamond patterns of numbers and the C program to print diamond patterns of stars.
Related Posts:


