13. Write a program in c language to print the pattern of a diamond using a for loop.
Pattern problems in c programming are one of the most common problems faced by programmers. This tutorial article includes diamond pattern problem in C language with approach, algorithm and related codes.
Problem Description
Write a C program that prints a diamond pattern given the number of rows as input.
Problem Solution
1.Take the row number as input.
2.Save the number in variable.
3.In the loop, print a space and then a star.
4.Repeat this process until their number reaches 0.
5.stop.
*
***
*****
*******
*********
*******
*****
***
*
The program code in C to show diamond pattern using for loop
/*
* Program to show Diamond Pattern using For Loop in c
*/
#include<stdio.h>
int main()
{
int number, i, k, count = 1;
printf("Enter number of rows: \n");
scanf("%d", &number);
count = number - 1;
for (k = 1; k <= number; k++)
{
for (i = 1; i <= count; i++)
printf(" ");
count--;
for (i = 1; i <= 2 * k - 1; i++)
printf("*");
printf("\n");
}
count = 1;
for (k = 1; k <= number - 1; k++)
{
for (i = 1; i <= count; i++)
printf(" ");
count++;
for (i = 1 ; i <= 2 *(number - k)- 1; i++)
printf("*");
printf("\n");
}
return(0);
}