Write a program in C programming language to print the diamond pattern as the output of the C program. Below is the code of the C program –
/* C program to print diamond as output*/ #include<stdio.h> #include<conio.h> main() { int n,i,j,k; printf("enter number of rows in odd number"); scanf("%d",&n); for(i=0;i<=(n/2);i++) { for(j=0;j<(n/2)-i;j++) { printf(" "); } for(k=0;k<(2*i)+1;k++) { printf("*"); } printf("\n"); } for(i=0;i<n/2;i++) { for(j=0;j<i+1;j++) { printf(" "); } for(k=0;k<n-2-(2*i);k++) { printf("*"); } printf("\n"); } getch(); }
From the above code of the C program you will get the output as follows –
very good and simly method is used in program so we are all greatful to yuo…
Hey, It is not the exact logic for a given program.
Here i m giving the code with minimum number of executing steps.
also i m sure that there doesn’t exist any other way to print the same Diamond such that code executing steps will be less than mine.
It is not copied from any website, book etc.
#include
void main()
{
int n,i,s,k,flagReverce=0;
printf(“Enter the no. of rows\n”);
scanf(“%d”, &n);
for (i=1; i>=1; i++)
{
for (s=n; s>i; s–)
printf(” “);
for(k=1; k<(i*2)-1; k++)
printf("*")
printf("\n")
if (i == n)
flagReverce = 1
if (flagReverce = 1)
i-=2
}
}
It’s nice. Thank you.
Kiran is there any other easy code..
Hi Yuvraj,
I did see your code which is very fine than other loopy huge programs and here is mine.
#include
#define NOR 5
int main()
{
char ucRow = 0;
char cStr = 0;
char cSpace = 0;
for(ucRow = -1 * (2*NOR – 2); ucRow <= (2*NOR – 2); ucRow+=2)
{
printf("\n");
for(cSpace = 0; cSpace < abs(ucRow) / 2; cSpace++)
printf(" ");
for(cStr = 0; cStr < ((2*NOR – 1) – abs(ucRow)); cStr++)
printf("*");
}
return !printf("\n");
}
I have checked your site and i have found some duplicate
content, that’s why you don’t rank high in google’s search results, but there is a tool that
can help you to create 100% unique content,
search for: Boorfe’s tips unlimited content