C Program
C Program to Display a Diamond Pattern Using Asterisks
Example:
C Program to Display a Diamond Pattern Using Asterisks
Explain Program
Step :1
#include <stdio.h>
input-output use printf() & scanf() function.
Step :2
int main()
The entry point of the C program. All execution .
Step :3
int n, a, k;
Three integer variables are declared:
Step :4
printf("Enter number of rows\n");
scanf("%d", &n);
user to enter an integer (n).
numbers determines the height of the top half diamond.
Step :5
for (k = 1; k <= n; k++)
{
for (a = 1; a <=
n-k; a++)
printf(" ");
for (a = 1; a <=
2*k-1; a++)
printf("*");
printf("\n");
}
This loop print the upper half of the diamond
Each iteration of k represents one row.
Prints the stars on each line.
Step :6
printf("\n");
After each row
Step :7
for (k = 1; k <= n - 1; k++)
{
for (a = 1; a <=
k; a++)
printf("
");
printf("*");
}
Outer loops
(k = 1 to n - 1)
1 to n - 1 to the middles row already
printed to the top part.
Prints leading spaces, which increase with each row.
Prints the stars.
No comments:
Post a Comment