Q. Write a C program to print the following number structure:
*
***
*****
*******
*****
***
*
Ans.
/*c program for symbol pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,r,c,sp;
printf("Enter number of rows : ");
scanf("%d",&num);
for(r=1; r<=num; r++)
{
for(sp=num-r; sp>=1; sp--)
printf(" ");
for(c=1; c<=r; c++)
printf("*");
for(c=r-1; c>=1; c--)
printf("*");
printf("\n");
}
for(r=1; r<=num; r++)
{
for(sp=r; sp>=1; sp--)
printf(" ");
for(c=1; c<=(num-r); c++)
printf("*");
for(c=num-r-1; c>=1; c--)
printf("*");
printf("\n");
}
getch();
return 0;
}
/*********** OUTPUT ************/
*
***
*****
*******
*****
***
*
Ans.
/*c program for symbol pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,r,c,sp;
printf("Enter number of rows : ");
scanf("%d",&num);
for(r=1; r<=num; r++)
{
for(sp=num-r; sp>=1; sp--)
printf(" ");
for(c=1; c<=r; c++)
printf("*");
for(c=r-1; c>=1; c--)
printf("*");
printf("\n");
}
for(r=1; r<=num; r++)
{
for(sp=r; sp>=1; sp--)
printf(" ");
for(c=1; c<=(num-r); c++)
printf("*");
for(c=num-r-1; c>=1; c--)
printf("*");
printf("\n");
}
getch();
return 0;
}
/*********** OUTPUT ************/
Thank you Sooooooooo much :)
ReplyDeleteThis is the best rhombus program without a single error...
ReplyDeleteThank u very much!!!
could u please provide algorithms and for flowchart for this program?
ReplyDeletethanku..........
ReplyDeleteRow number does not match, if you give input 7, the compiler gives 13 line as output,
ReplyDeletebcs, the compiler gets input 7+inside code 7-1=6, so, the compiler gives 7+6=13 lines.
It's an error, can you explain plz... ... ... @ Admin
@Jack DSauza,
DeleteIn above program we are making rhombus through lines (i.e. rows),
Let's say user give input 3 then its mean that top part of rhombus will be make in three lines. And here no matter how much symbols comes in the columns.
Hence, all the calculation in above symbol rhombus program calculated through line(rows) not column.
This is a program in terms of the number of columns:-
ReplyDelete/*
*
***
*****
*******
*****
***
*
*/
#include
int main()
{
int m=42;
printf("enter the number of columns\n");
int num;
scanf("%d",&num);
for(int i=num;i>=1;i-=2)
{
for(int j=(i-1)/2;j>=1;j--)
{
printf(" ");
}
for(int k=num-i+1;k>=1;k--)
{
printf("%c",(char)m);
}
printf("\n");
}
for(int i=num-2;i>=1;i-=2)
{
for(int j=(num-i)/2;j>=1;j--)
{
printf(" ");
}
for(int k=i;k>=1;k--)
{
printf("%c",(char)m);
}
printf("\n");
}
return 0;
}
*
ReplyDelete***
*****
*******
*****
***
*
can u use only two for loops and if condition
This comment has been removed by the author.
ReplyDeleteHow to print this if input is given as 3
ReplyDelete0
010
01210
0123210
01210
010
0
@ REMO JOHNSON
Deleteyour required number pyramid C program source code as following:
#include"stdio.h"
int main()
{
int r,c,num=3,n;
for(r=0; r<=num; r++)
{
for(c=0; c<=r; c++)
printf("%d",c);
for(c=r-1; c>=0; c--)
printf("%d",c);
printf("\n");
}
for(r=1,n=num; r<=num; r++,n--)
{
for(c=0; c<=n-1; c++)
printf("%d",c);
for(c=num-r-1; c>0; c--)
printf("%d",c);
printf("\n");
}
getch();
return 0;
}
Hey how do you create the option in the output by using a function, void displayRhombus(int diagonal, char fillCharacter) ? : I want to create an option for 'enter the character and enter the number of rows'!
ReplyDeleteProgram execution sample:
Please enter the character: #
Please enter the diagonal of the rhombus: 7
#
###
#####
#######
#####
###
#
For example, if the function call is
displayRhombus(3, ‘#’), then it should display:
#
###
#
Or, if the function call is displayRhombus(5, ‘$’), then it should display:
$
$$$
$$$$$
$$$
$